@@ -2,6 +2,7 @@ package mono
2
2
3
3
import (
4
4
"context"
5
+ "math"
5
6
"sync"
6
7
"sync/atomic"
7
8
@@ -10,10 +11,27 @@ import (
10
11
"github.com/pkg/errors"
11
12
)
12
13
13
- var _sinkPool = sync.Pool {
14
- New : func () interface {} {
15
- return new (sink )
16
- },
14
+ var globalSinkPool sinkPool
15
+
16
+ type sinkPool struct {
17
+ inner sync.Pool
18
+ }
19
+
20
+ func (p * sinkPool ) get () * sink {
21
+ if exist , _ := p .inner .Get ().(* sink ); exist != nil {
22
+ atomic .StoreInt32 (& exist .stat , 0 )
23
+ return exist
24
+ }
25
+ return & sink {}
26
+ }
27
+
28
+ func (p * sinkPool ) put (s * sink ) {
29
+ if s == nil {
30
+ return
31
+ }
32
+ s .actual = nil
33
+ atomic .StoreInt32 (& s .stat , math .MinInt32 )
34
+ p .inner .Put (s )
17
35
}
18
36
19
37
type Sink interface {
@@ -30,18 +48,6 @@ type sink struct {
30
48
stat int32
31
49
}
32
50
33
- func borrowSink (sub reactor.Subscriber ) * sink {
34
- s := _sinkPool .Get ().(* sink )
35
- atomic .StoreInt32 (& s .stat , 0 )
36
- s .actual = sub
37
- return s
38
- }
39
-
40
- func returnSink (s * sink ) {
41
- s .actual = nil
42
- _sinkPool .Put (s )
43
- }
44
-
45
51
func newMonoCreate (gen func (context.Context , Sink )) monoCreate {
46
52
return monoCreate {
47
53
sinker : func (ctx context.Context , sink Sink ) {
@@ -79,29 +85,27 @@ func (s *sink) Success(v Any) {
79
85
}
80
86
81
87
func (s * sink ) Request (n int ) {
82
- if n < 1 {
83
- panic (reactor .ErrNegativeRequest )
84
- }
88
+ // ignore
85
89
}
86
90
87
91
func (s * sink ) Cancel () {
88
92
atomic .CompareAndSwapInt32 (& s .stat , 0 , statCancel )
89
93
}
90
94
91
95
func (s * sink ) Complete () {
92
- defer returnSink (s )
96
+ defer globalSinkPool . put (s )
93
97
if atomic .CompareAndSwapInt32 (& s .stat , 0 , statComplete ) {
94
98
s .actual .OnComplete ()
95
99
}
96
100
}
97
101
98
102
func (s * sink ) Error (err error ) {
99
- defer returnSink (s )
100
- if atomic .CompareAndSwapInt32 (& s .stat , 0 , statError ) {
101
- s . actual . OnError (err )
103
+ defer globalSinkPool . put (s )
104
+ if ! atomic .CompareAndSwapInt32 (& s .stat , 0 , statError ) {
105
+ hooks . Global (). OnErrorDrop (err )
102
106
return
103
107
}
104
- hooks . Global (). OnErrorDrop (err )
108
+ s . actual . OnError (err )
105
109
}
106
110
107
111
func (s * sink ) Next (v Any ) {
@@ -120,7 +124,8 @@ func (s *sink) Next(v Any) {
120
124
}
121
125
122
126
func (m monoCreate ) SubscribeWith (ctx context.Context , s reactor.Subscriber ) {
123
- sink := borrowSink (s )
124
- s .OnSubscribe (ctx , sink )
125
- m .sinker (ctx , sink )
127
+ sk := globalSinkPool .get ()
128
+ sk .actual = s
129
+ s .OnSubscribe (ctx , sk )
130
+ m .sinker (ctx , sk )
126
131
}
0 commit comments