-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrap.go
More file actions
40 lines (34 loc) · 909 Bytes
/
Copy pathwrap.go
File metadata and controls
40 lines (34 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package workerpool
import (
"context"
"sync"
)
// None is a placeholder for convinience if there is no parameters or no return value.
type None struct{}
type wrapResult[T any] struct {
val T
err error
}
// Wrap wraps a function for ease of future use,
// allowing the wrapped function to be executed within the WorkerPool.
func Wrap[In, Out any](p *WorkerPool, f func(context.Context, In) (Out, error)) func(context.Context, In) (Out, error) {
chanPool := sync.Pool{}
return func(ctx context.Context, in In) (Out, error) {
c, ok := chanPool.Get().(chan wrapResult[Out])
if !ok {
c = make(chan wrapResult[Out], 1)
}
err := p.Submit(ctx, func(innerctx context.Context) {
out, err := f(innerctx, in)
c <- wrapResult[Out]{val: out, err: err}
})
if err != nil {
chanPool.Put(c)
var out Out
return out, err
}
ret := <-c
chanPool.Put(c)
return ret.val, ret.err
}
}