Skip to content

Add a semaphore to limit concurrent t.me access#11

Merged
nDmitry merged 1 commit into
mainfrom
scraper-semaphore
Jan 13, 2026
Merged

Add a semaphore to limit concurrent t.me access#11
nDmitry merged 1 commit into
mainfrom
scraper-semaphore

Conversation

@nDmitry
Copy link
Copy Markdown
Owner

@nDmitry nDmitry commented Jan 13, 2026

Summary by CodeRabbit

  • Chores
    • Enhanced scraper efficiency by implementing request concurrency limiting to prevent resource exhaustion and improve overall system stability.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 13, 2026

Warning

Rate limit exceeded

@nDmitry has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 17 minutes and 43 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between bd43b84 and d7996ed.

📒 Files selected for processing (1)
  • internal/feed/scraper.go

Walkthrough

The scraper now enforces concurrent request limits using a semaphore mechanism capped at 3 simultaneous requests. A new semaphore type with acquire and release methods was introduced, integrated into the Scraper struct, and initialized during instantiation to control HTTP visit concurrency.

Changes

Cohort / File(s) Summary
Concurrency limiting mechanism
internal/feed/scraper.go
Added new semaphore type with constructor and acquire/release methods; Scraper struct now includes unexported sema field; NewDefaultScraper initializes semaphore with limit of 3 concurrent requests; HTTP visit operations wrapped with semaphore acquire/release calls.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A scraper hops with grace so fine,
With three requests in sync—divine!
The semaphore stands guard with care,
No congestion in the air! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding a semaphore to limit concurrent requests to t.me (Telegram), which is exactly what the code changes implement.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
internal/feed/scraper.go (1)

23-37: Consider adding context support to acquire to prevent goroutine leaks.

The current acquire() blocks unconditionally. If a caller's context is cancelled while waiting for the semaphore, the goroutine will block forever, potentially causing leaks under cancellation scenarios.

♻️ Suggested context-aware acquire
-func (s *semaphore) acquire() {
-	s.c <- struct{}{}
+func (s *semaphore) acquire(ctx context.Context) error {
+	select {
+	case s.c <- struct{}{}:
+		return nil
+	case <-ctx.Done():
+		return ctx.Err()
+	}
 }

Then update the call site in Scrape:

-	s.sema.acquire()
-	defer s.sema.release()
-
-	if err := c.Visit(channel.URL); err != nil {
+	if err := s.sema.acquire(ctx); err != nil {
+		return nil, fmt.Errorf("could not acquire semaphore: %w", err)
+	}
+	defer s.sema.release()
+
+	if err := c.Visit(channel.URL); err != nil {
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6dd19a3 and bd43b84.

📒 Files selected for processing (1)
  • internal/feed/scraper.go
🔇 Additional comments (3)
internal/feed/scraper.go (3)

21-22: LGTM!

Reasonable concurrency limit constant.


39-51: LGTM!

Clean integration of the semaphore into the Scraper struct with proper initialization.


95-100: Good use of defer for release.

The placement correctly limits concurrent Visit calls, and defer ensures the semaphore is released on all exit paths including errors.

@nDmitry nDmitry merged commit 1975dd4 into main Jan 13, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant