auto-download model in dit.New()#48
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughThis PR centralizes model download logic in the dit library: it adds a canonical ChangesModel Download Consolidation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related issues
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
dit.go (1)
71-82: ⚖️ Poor tradeoffConcurrent
New()calls may trigger parallel downloads.If multiple goroutines call
New()simultaneously on first use, each could pass theFindModelcheck and start downloading to the same destination file. This could cause file corruption or redundant downloads.Consider using
sync.Onceor a file lock to serialize the download:♻️ Example fix using sync.Once
+import "sync" + +var downloadOnce sync.Once +var downloadErr error func New() (*Classifier, error) { if path, err := FindModel("model.json"); err == nil { return Load(path) } dest := filepath.Join(ModelDir(), "model.json") - slog.Info("Model not found, downloading", "url", ModelURL, "dest", dest) - if err := Download(dest); err != nil { - return nil, fmt.Errorf("dit: %w", err) - } + downloadOnce.Do(func() { + slog.Info("Model not found, downloading", "url", ModelURL, "dest", dest) + downloadErr = Download(dest) + }) + if downloadErr != nil { + return nil, fmt.Errorf("dit: %w", downloadErr) + } return Load(dest) }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@dit.go` around lines 71 - 82, Concurrent calls to New() can all pass FindModel and start Download to the same dest; serialize the first-time download by introducing a package-level guard (e.g., a sync.Once or a file lock) around the FindModel -> Download -> Load sequence so only one goroutine performs the download while others wait and then call Load; update New() to use that guard (referencing New, FindModel, Download, Load, ModelDir, ModelURL) and ensure any Download error is propagated to callers and subsequent callers re-check FindModel/Load after the guarded operation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@dit.go`:
- Around line 91-94: The call to http.Get(ModelURL) uses the default client with
no timeout and can hang; replace it with an HTTP request using a cancellable
context or an http.Client with a configured Timeout (e.g., context.WithTimeout +
http.NewRequestWithContext or a client := &http.Client{Timeout: ...} then
client.Do) to bound the total wait; update the request usage where
http.Get(ModelURL) appears (and keep proper resp.Body.Close and error wrapping
like fmt.Errorf("download model: %w", err)) so the download will fail fast on
slow/blocked servers.
---
Nitpick comments:
In `@dit.go`:
- Around line 71-82: Concurrent calls to New() can all pass FindModel and start
Download to the same dest; serialize the first-time download by introducing a
package-level guard (e.g., a sync.Once or a file lock) around the FindModel ->
Download -> Load sequence so only one goroutine performs the download while
others wait and then call Load; update New() to use that guard (referencing New,
FindModel, Download, Load, ModelDir, ModelURL) and ensure any Download error is
propagated to callers and subsequent callers re-check FindModel/Load after the
guarded operation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 6429ead2-54bb-4ac8-808d-80a15df80074
📒 Files selected for processing (5)
README.mddit.gointernal/cli/data.gointernal/cli/run.gointernal/cli/up.go
Closes #47. Refs projectdiscovery/httpx#2495.
dit.New()now downloadsmodel.jsonto~/.dit/on miss; library users no longer need to reimplement what the CLI was doing.Summary by CodeRabbit
New Features
Documentation