|
4 | 4 | "encoding/json" |
5 | 5 | "net/http" |
6 | 6 | "strings" |
| 7 | + "sync" |
7 | 8 | "time" |
8 | 9 |
|
9 | 10 | "github.com/huangsam/namigo/internal/model" |
@@ -35,30 +36,45 @@ func SearchByAPI(name string, max int) ([]model.PyPIPackageResult, error) { |
35 | 36 | }() |
36 | 37 |
|
37 | 38 | 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 | + }() |
61 | 75 | } |
62 | 76 |
|
| 77 | + wg.Wait() |
| 78 | + |
63 | 79 | return result, nil |
64 | 80 | } |
0 commit comments