-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.go
80 lines (65 loc) · 1.37 KB
/
pool.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package centrifuge
import (
"context"
"errors"
"sync"
"github.com/roadrunner-server/goridge/v3/pkg/frame"
"github.com/roadrunner-server/pool/payload"
)
type wrapper struct {
stopChPool sync.Pool
mu *sync.RWMutex
pool Pool
}
func newPoolMuWrapper(pool Pool, mu *sync.RWMutex) *wrapper {
return &wrapper{
stopChPool: sync.Pool{
New: func() any {
return make(chan struct{}, 1)
},
},
mu: mu,
pool: pool,
}
}
func (p *wrapper) Exec(ctx context.Context, pld *payload.Payload) (*payload.Payload, error) {
p.mu.RLock()
sc := p.getStopCh()
re, err := p.pool.Exec(ctx, pld, sc)
p.mu.RUnlock()
if err != nil {
p.putStopCh(sc)
return nil, err
}
var resp *payload.Payload
select {
case pl := <-re:
if pl.Error() != nil {
p.putStopCh(sc)
return nil, pl.Error()
}
// streaming is not supported
if pl.Payload().Flags&frame.STREAM != 0 {
// stop the stream, do not return the channel
sc <- struct{}{}
return nil, errors.New("streaming response not supported")
}
// assign the payload
resp = pl.Payload()
default:
p.putStopCh(sc)
return nil, errors.New("worker empty response")
}
p.putStopCh(sc)
return resp, nil
}
func (p *wrapper) getStopCh() chan struct{} {
return p.stopChPool.Get().(chan struct{})
}
func (p *wrapper) putStopCh(ch chan struct{}) {
select {
case <-ch:
default:
}
p.stopChPool.Put(ch)
}