Skip to content

Commit 7e09b7f

Browse files
committed
✨Can you read https://mfbmina.dev/en/posts/waitgroups/ and apply the new function wg.Go in sponge?
1 parent 2a9bca1 commit 7e09b7f

7 files changed

Lines changed: 14 additions & 26 deletions

File tree

cmd/sponge/commands/assistant/woker.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ func NewWorkerPool(ctx context.Context, workerSize int, jobQueueSize int) (*Work
6262
// Start starts the worker pool
6363
func (wp *WorkerPool) Start() {
6464
for i := 0; i < wp.workerCount; i++ {
65-
wp.wg.Add(1)
66-
go wp.worker()
65+
wp.wg.Go(func() { wp.worker() })
6766
}
6867
}
6968

7069
// worker is the goroutine that actually executes tasks
7170
func (wp *WorkerPool) worker() {
72-
defer wp.wg.Done()
7371
for {
7472
select {
7573
case job, ok := <-wp.jobQueue:

cmd/sponge/commands/perftest/common/progress_bar.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ func NewTimeBar(totalDuration time.Duration) *TimeBar {
147147
// Start begins automatic updates in a background goroutine.
148148
func (b *TimeBar) Start() {
149149
b.startTime = time.Now()
150-
b.wg.Add(1)
151-
go b.run()
150+
b.wg.Go(func() { b.run() })
152151
}
153152

154153
// stopped is an internal helper to handle shutting down the progress bar.
@@ -178,8 +177,6 @@ func (b *TimeBar) Stop() {
178177

179178
// run periodically refreshes the bar in the background.
180179
func (b *TimeBar) run() {
181-
defer b.wg.Done()
182-
183180
ticker := time.NewTicker(500 * time.Millisecond)
184181
defer ticker.Stop()
185182

cmd/sponge/commands/perftest/http/run.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,12 @@ func (p *PerfTestHTTP) RunWithFixedRequestsNum(globalCtx context.Context) (*Stat
125125
}
126126

127127
for i := 0; i < p.Worker; i++ {
128-
wg.Add(1)
129-
go func() {
130-
defer wg.Done()
128+
wg.Go(func() {
131129
for range jobs {
132130
requestOnce(p.Client, p.Params, resultCh)
133131
bar.Increment()
134132
}
135-
}()
133+
})
136134
}
137135

138136
start = time.Now()
@@ -214,19 +212,17 @@ func (p *PerfTestHTTP) RunWithFixedDuration(globalCtx context.Context) (*Statist
214212

215213
// Start workers
216214
for i := 0; i < p.Worker; i++ {
217-
wg.Add(1)
218-
go func() {
219-
defer wg.Done()
215+
wg.Go(func() {
220216
// Keep sending requests until the context is canceled
221217
for {
222218
select {
223219
case <-ctx.Done():
224-
return // Exit goroutine when context is canceled
220+
return
225221
default:
226222
requestOnce(p.Client, p.Params, resultCh)
227223
}
228224
}
229-
}()
225+
})
230226
}
231227

232228
start = time.Now()

cmd/sponge/commands/perftest/websocket/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ func (c *Client) Dial(ctx context.Context) error {
6161
}
6262

6363
// Run starts the client worker.
64-
func (c *Client) Run(ctx context.Context, wg *sync.WaitGroup) {
65-
defer wg.Done()
64+
func (c *Client) Run(ctx context.Context) {
6665

6766
err := c.Dial(ctx)
6867
if err != nil {

cmd/sponge/commands/perftest/websocket/websocket.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ func (p *perfTestParams) run() error {
149149
break
150150
}
151151

152-
wg.Add(1)
153152
client := NewClient(i+1, p.targetURL, stats, p.sendInterval, p.payloadData, p.isJSON)
154-
go client.Run(mainCtx, &wg)
153+
wg.Go(func() { client.Run(mainCtx) })
155154

156155
if rampUpDelay > 0 {
157156
time.Sleep(rampUpDelay)

cmd/sponge/commands/plugins.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,9 @@ func installPlugins(lackNames []string) {
148148
continue
149149
}
150150

151-
wg.Add(1)
152-
go func(name string) {
153-
defer wg.Done()
154-
ctx, _ := context.WithTimeout(context.Background(), time.Minute*3) //nolint
151+
wg.Go(func() {
152+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*3)
153+
defer cancel()
155154
pkgAddr, ok := installPluginCommands[name]
156155
if !ok {
157156
return
@@ -166,7 +165,7 @@ func installPlugins(lackNames []string) {
166165
} else {
167166
fmt.Printf("%s %s\n", installedSymbol, name)
168167
}
169-
}(name)
168+
})
170169
}
171170

172171
wg.Wait()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/go-dev-frame/sponge
22

3-
go 1.23.0
3+
go 1.25.0
44

55
require (
66
github.com/DATA-DOG/go-sqlmock v1.5.0

0 commit comments

Comments
 (0)