Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: speed up ReleaseTimeout() for multi-pool #332

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions multipool.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"strings"
"sync/atomic"
"time"

"golang.org/x/sync/errgroup"
)

// LoadBalancingStrategy represents the type of load-balancing algorithm.
Expand Down Expand Up @@ -182,22 +184,43 @@
return ErrPoolClosed
}

var errStr strings.Builder
errCh := make(chan error, len(mp.pools))
var wg errgroup.Group
for i, pool := range mp.pools {
if err := pool.ReleaseTimeout(timeout); err != nil {
errStr.WriteString(fmt.Sprintf("pool %d: %v\n", i, err))
if i < len(mp.pools)-1 {
errStr.WriteString(" | ")
}
return err
func(p *Pool, idx int) {
wg.Go(func() error {
err := p.ReleaseTimeout(timeout)
if err != nil {
err = fmt.Errorf("pool %d: %v", idx, err)

Check warning on line 194 in multipool.go

View check run for this annotation

Codecov / codecov/patch

multipool.go#L194

Added line #L194 was not covered by tests
}
errCh <- err
return err
})
}(pool, i)
}

_ = wg.Wait()

var (
i int
errStr strings.Builder
)
for err := range errCh {
i++
if i == len(mp.pools) {
break
}
if err != nil {
errStr.WriteString(err.Error())
errStr.WriteString(" | ")

Check warning on line 215 in multipool.go

View check run for this annotation

Codecov / codecov/patch

multipool.go#L214-L215

Added lines #L214 - L215 were not covered by tests
}
}

if errStr.Len() == 0 {
return nil
}

return errors.New(errStr.String())
return errors.New(strings.TrimSuffix(errStr.String(), " | "))

Check warning on line 223 in multipool.go

View check run for this annotation

Codecov / codecov/patch

multipool.go#L223

Added line #L223 was not covered by tests
}

// Reboot reboots a released multi-pool.
Expand Down
39 changes: 31 additions & 8 deletions multipool_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"strings"
"sync/atomic"
"time"

"golang.org/x/sync/errgroup"
)

// MultiPoolWithFunc consists of multiple pools, from which you will benefit the
Expand Down Expand Up @@ -172,22 +174,43 @@
return ErrPoolClosed
}

var errStr strings.Builder
errCh := make(chan error, len(mp.pools))
var wg errgroup.Group
for i, pool := range mp.pools {
if err := pool.ReleaseTimeout(timeout); err != nil {
errStr.WriteString(fmt.Sprintf("pool %d: %v\n", i, err))
if i < len(mp.pools)-1 {
errStr.WriteString(" | ")
}
return err
func(p *PoolWithFunc, idx int) {
wg.Go(func() error {
err := p.ReleaseTimeout(timeout)
if err != nil {
err = fmt.Errorf("pool %d: %v", idx, err)

Check warning on line 184 in multipool_func.go

View check run for this annotation

Codecov / codecov/patch

multipool_func.go#L184

Added line #L184 was not covered by tests
}
errCh <- err
return err
})
}(pool, i)
}

_ = wg.Wait()

var (
i int
errStr strings.Builder
)
for err := range errCh {
i++
if i == len(mp.pools) {
break
}
if err != nil {
errStr.WriteString(err.Error())
errStr.WriteString(" | ")

Check warning on line 205 in multipool_func.go

View check run for this annotation

Codecov / codecov/patch

multipool_func.go#L204-L205

Added lines #L204 - L205 were not covered by tests
}
}

if errStr.Len() == 0 {
return nil
}

return errors.New(errStr.String())
return errors.New(strings.TrimSuffix(errStr.String(), " | "))

Check warning on line 213 in multipool_func.go

View check run for this annotation

Codecov / codecov/patch

multipool_func.go#L213

Added line #L213 was not covered by tests
}

// Reboot reboots a released multi-pool.
Expand Down
Loading