Skip to content

Commit 28caeb8

Browse files
committed
Fix concurrency bugs
1 parent 71216c1 commit 28caeb8

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

pkg/dns/search.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
)
1010

1111
func SearchByProbe(name string, max int) ([]model.DNSResult, error) {
12-
var wg sync.WaitGroup
1312
domains := []string{"com", "org", "net", "io", "tech", "ai", "me", "shop"}
1413
domainChan := make(chan string)
1514

@@ -21,8 +20,10 @@ func SearchByProbe(name string, max int) ([]model.DNSResult, error) {
2120
}()
2221

2322
result := []model.DNSResult{}
23+
resultCount := 0
2424
errorCount := 0
2525
var mu sync.Mutex
26+
var wg sync.WaitGroup
2627

2728
for i := 0; i < 4; i++ {
2829
wg.Add(1)
@@ -35,11 +36,14 @@ func SearchByProbe(name string, max int) ([]model.DNSResult, error) {
3536
errorCount++
3637
}
3738
mu.Lock()
38-
result = append(result, model.DNSResult{FQDN: fullDomain, IPList: ips})
39+
if resultCount < max {
40+
result = append(result, model.DNSResult{FQDN: fullDomain, IPList: ips})
41+
resultCount++
42+
}
43+
mu.Unlock()
3944
if len(result) >= max {
4045
return
4146
}
42-
mu.Unlock()
4347
}
4448
}()
4549
}

pkg/pypi/search.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"net/http"
66
"strings"
7+
"sync"
78
"time"
89

910
"github.com/huangsam/namigo/internal/model"
@@ -35,30 +36,45 @@ func SearchByAPI(name string, max int) ([]model.PyPIPackageResult, error) {
3536
}()
3637

3738
result := []model.PyPIPackageResult{}
38-
count := 0
39-
for pkg := range taskChan {
40-
bd, err := util.RESTAPIQuery(client, detail(pkg))
41-
if err != nil {
42-
continue
43-
}
44-
var detailRes PypiDetailResponse
45-
if err := json.Unmarshal(bd, &detailRes); err != nil {
46-
continue
47-
}
48-
description := detailRes.Info.Summary
49-
if len(description) == 0 {
50-
description = model.NoDescription
51-
}
52-
author := detailRes.Info.Author
53-
if len(author) == 0 {
54-
author = model.NoAuthor
55-
}
56-
result = append(result, model.PyPIPackageResult{Name: pkg, Description: description, Author: author})
57-
count++
58-
if count >= max {
59-
break
60-
}
39+
resultCount := 0
40+
var mu sync.Mutex
41+
var wg sync.WaitGroup
42+
43+
for i := 0; i < 4; i++ {
44+
wg.Add(1)
45+
go func() {
46+
defer wg.Done()
47+
for pkg := range taskChan {
48+
bd, err := util.RESTAPIQuery(client, detail(pkg))
49+
if err != nil {
50+
continue
51+
}
52+
var detailRes PypiDetailResponse
53+
if err := json.Unmarshal(bd, &detailRes); err != nil {
54+
continue
55+
}
56+
description := detailRes.Info.Summary
57+
if len(description) == 0 {
58+
description = model.NoDescription
59+
}
60+
author := detailRes.Info.Author
61+
if len(author) == 0 {
62+
author = model.NoAuthor
63+
}
64+
mu.Lock()
65+
if resultCount < max {
66+
result = append(result, model.PyPIPackageResult{Name: pkg, Description: description, Author: author})
67+
resultCount++
68+
}
69+
mu.Unlock()
70+
if resultCount >= max {
71+
break
72+
}
73+
}
74+
}()
6175
}
6276

77+
wg.Wait()
78+
6379
return result, nil
6480
}

0 commit comments

Comments
 (0)