Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion services/url/cmd/url/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ func main() {
notificationProducer.Start(ctx)

httpClient := &http.Client{Timeout: 5 * time.Second}
chkr := checker.NewURLChecker(urlSvc, l, httpClient, 1*time.Minute, notificationProducer, tracer)
concurrencyLimit := 10
chkr := checker.NewURLChecker(urlSvc, l, httpClient, 1*time.Minute, notificationProducer, tracer, concurrencyLimit)
go chkr.Start(ctx)

urlHandler := handler.NewURLHandler(urlSvc, l)
Expand Down
13 changes: 9 additions & 4 deletions services/url/internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type URLChecker struct {
interval time.Duration
notificationProducer kafka.NotificationProducer
tracer *tracing.Tracer
concurrencyLimit int
httpTimeOut time.Duration
}

func NewURLChecker(
Expand All @@ -37,6 +39,7 @@ func NewURLChecker(
interval time.Duration,
producer kafka.NotificationProducer,
tracer *tracing.Tracer,
concurrencyLimit int,
) *URLChecker {
if producer == nil {
// This panic indicates a serious configuration error that should be caught
Expand All @@ -52,6 +55,7 @@ func NewURLChecker(
interval: interval,
notificationProducer: producer,
tracer: tracer,
concurrencyLimit: concurrencyLimit,
}
}

Expand Down Expand Up @@ -79,12 +83,13 @@ func (uc *URLChecker) CheckAllURLs(ctx context.Context) {
urls, err := uc.svc.GetAll(ctx)
if err != nil {
uc.logger.Error("Failed to fetch URLs", slog.Any("error", err))
span.RecordError(err)
uc.tracer.RecordError(span, err)
return
}

var wg sync.WaitGroup
sem := make(chan struct{}, 10) // Limit to 10 concurrent checks
// Use the configurable concurrency limit for the semaphore.
sem := make(chan struct{}, uc.concurrencyLimit)

for _, url := range urls {
wg.Add(1)
Expand Down Expand Up @@ -114,7 +119,7 @@ func (uc *URLChecker) CheckAllURLs(ctx context.Context) {
slog.String("status", status),
slog.Any("error", err),
)
span.RecordError(err)
uc.tracer.RecordError(span, err)
} else {
uc.logger.Info("URL status updated",
slog.String("urlID", url.ID),
Expand All @@ -135,7 +140,7 @@ func (uc *URLChecker) CheckAllURLs(ctx context.Context) {
uc.logger.Error("Failed to publish notification",
slog.String("url_id", url.ID),
slog.Any("error", err))
span.RecordError(err)
uc.tracer.RecordError(span, err)
}
}
}
Expand Down
Loading