@@ -3,6 +3,7 @@ package socket
33import (
44 "context"
55 "sync"
6+ "sync/atomic"
67
78 "github.com/jjeffcaii/reactor-go"
89 "github.com/rsocket/rsocket-go/core"
@@ -12,20 +13,35 @@ import (
1213 "github.com/rsocket/rsocket-go/rx"
1314)
1415
15- var _requestResponseSubscriberPool = sync.Pool {
16- New : func () interface {} {
17- return new (requestResponseSubscriber )
18- },
16+ var globalRequestResponseSubscriberPool requestResponseSubscriberPool
17+
18+ type requestResponseSubscriberPool struct {
19+ inner sync.Pool
20+ }
21+
22+ func (p * requestResponseSubscriberPool ) get () * requestResponseSubscriber {
23+ if exist , _ := p .inner .Get ().(* requestResponseSubscriber ); exist != nil {
24+ return exist
25+ }
26+ return & requestResponseSubscriber {}
27+ }
28+
29+ func (p * requestResponseSubscriberPool ) put (s * requestResponseSubscriber ) {
30+ if s == nil {
31+ return
32+ }
33+ p .inner .Put (s )
1934}
2035
2136type requestResponseSubscriber struct {
2237 dc * DuplexConnection
2338 sid uint32
2439 receiving fragmentation.HeaderAndPayload
40+ sndCnt int32
2541}
2642
2743func borrowRequestResponseSubscriber (dc * DuplexConnection , sid uint32 , receiving fragmentation.HeaderAndPayload ) rx.Subscriber {
28- s := _requestResponseSubscriberPool . Get ().( * requestResponseSubscriber )
44+ s := globalRequestResponseSubscriberPool . get ( )
2945 s .receiving = receiving
3046 s .dc = dc
3147 s .sid = sid
@@ -39,11 +55,13 @@ func returnRequestResponseSubscriber(s rx.Subscriber) {
3955 }
4056 actual .dc = nil
4157 actual .receiving = nil
42- _requestResponseSubscriberPool .Put (actual )
58+ actual .sndCnt = 0
59+ globalRequestResponseSubscriberPool .put (actual )
4360}
4461
4562func (r * requestResponseSubscriber ) OnNext (next payload.Payload ) {
4663 r .dc .sendPayload (r .sid , next , core .FlagNext | core .FlagComplete )
64+ atomic .AddInt32 (& r .sndCnt , 1 )
4765}
4866
4967func (r * requestResponseSubscriber ) OnError (err error ) {
@@ -55,6 +73,9 @@ func (r *requestResponseSubscriber) OnError(err error) {
5573}
5674
5775func (r * requestResponseSubscriber ) OnComplete () {
76+ if atomic .AddInt32 (& r .sndCnt , 1 ) == 1 {
77+ r .dc .sendPayload (r .sid , payload .Empty (), core .FlagComplete )
78+ }
5879 r .dc .unregister (r .sid )
5980 r .finish ()
6081}
0 commit comments