1818)
1919
2020const (
21- DefaultN = 20
22- defaultHandshakeTimeout = 100 * time .Millisecond
23- defaultResendTimeout = 100 * time .Millisecond
24- finSendTimeout = 1000 * time .Millisecond
25- DefaultSendTimeout = math .MaxInt64
26- DefaultRecvTimeout = math .MaxInt64
21+ DefaultN = 20
2722)
2823
2924type sendBytesFunc func (ctx context.Context , b []byte ) error
@@ -58,30 +53,16 @@ type GoBackNConn struct {
5853 // sequence that we have received.
5954 recvSeq uint8
6055
61- // resendTimeout is the duration that will be waited before resending
62- // the packets in the current queue.
63- resendTimeout time.Duration
64- resendTicker * time.Ticker
56+ resendTicker * time.Ticker
6557
6658 recvFromStream recvBytesFunc
6759 sendToStream sendBytesFunc
6860
6961 recvDataChan chan * PacketData
7062 sendDataChan chan * PacketData
7163
72- sendTimeout time.Duration
73- sendTimeoutMu sync.RWMutex
74-
75- recvTimeout time.Duration
76- recvTimeoutMu sync.RWMutex
77-
7864 isServer bool
7965
80- // handshakeTimeout is the time after which the server or client
81- // will abort and restart the handshake if the expected response is
82- // not received from the peer.
83- handshakeTimeout time.Duration
84-
8566 // receivedACKSignal channel is used to signal that the queue size has
8667 // been decreased.
8768 receivedACKSignal chan struct {}
@@ -109,6 +90,10 @@ type GoBackNConn struct {
10990 quit chan struct {}
11091 closeOnce sync.Once
11192 wg sync.WaitGroup
93+
94+ // timeoutManager is used to manage all the timeouts used by the
95+ // GoBackNConn.
96+ timeoutManager * TimeoutManager
11297}
11398
11499// newGoBackNConn creates a GoBackNConn instance with all the members which
@@ -118,51 +103,46 @@ func newGoBackNConn(ctx context.Context, sendFunc sendBytesFunc,
118103
119104 ctxc , cancel := context .WithCancel (ctx )
120105
106+ timeoutManager := NewTimeOutManager (isServer )
107+
108+ queue := newQueue (n + 1 , timeoutManager )
109+
121110 return & GoBackNConn {
122111 n : n ,
123112 s : n + 1 ,
124- resendTimeout : defaultResendTimeout ,
125113 recvFromStream : recvFunc ,
126114 sendToStream : sendFunc ,
127115 recvDataChan : make (chan * PacketData , n ),
128116 sendDataChan : make (chan * PacketData ),
129117 isServer : isServer ,
130- sendQueue : newQueue (n + 1 , defaultHandshakeTimeout ),
131- handshakeTimeout : defaultHandshakeTimeout ,
132- recvTimeout : DefaultRecvTimeout ,
133- sendTimeout : DefaultSendTimeout ,
118+ sendQueue : queue ,
134119 receivedACKSignal : make (chan struct {}),
135120 resendSignal : make (chan struct {}, 1 ),
136121 remoteClosed : make (chan struct {}),
137122 ctx : ctxc ,
138123 cancel : cancel ,
139124 quit : make (chan struct {}),
125+ timeoutManager : timeoutManager ,
140126 }
141127}
142128
143- // setN sets the current N to use. This _must_ be set before the handshake is
144- // completed.
145- func (g * GoBackNConn ) setN (n uint8 ) {
146- g .n = n
147- g .s = n + 1
148- g .recvDataChan = make (chan * PacketData , n )
149- g .sendQueue = newQueue (n + 1 , defaultHandshakeTimeout )
150- }
151-
152129// SetSendTimeout sets the timeout used in the Send function.
153130func (g * GoBackNConn ) SetSendTimeout (timeout time.Duration ) {
154- g .sendTimeoutMu .Lock ()
155- defer g .sendTimeoutMu .Unlock ()
156-
157- g .sendTimeout = timeout
131+ g .timeoutManager .SetSendTimeout (timeout )
158132}
159133
160134// SetRecvTimeout sets the timeout used in the Recv function.
161135func (g * GoBackNConn ) SetRecvTimeout (timeout time.Duration ) {
162- g .recvTimeoutMu . Lock ( )
163- defer g . recvTimeoutMu . Unlock ()
136+ g .timeoutManager . SetRecvTimeout ( timeout )
137+ }
164138
165- g .recvTimeout = timeout
139+ // setN sets the current N to use. This _must_ be set before the handshake is
140+ // completed.
141+ func (g * GoBackNConn ) setN (n uint8 ) {
142+ g .n = n
143+ g .s = n + 1
144+ g .recvDataChan = make (chan * PacketData , n )
145+ g .sendQueue = newQueue (n + 1 , g .timeoutManager )
166146}
167147
168148// Send blocks until an ack is received for the packet sent N packets before.
@@ -174,9 +154,7 @@ func (g *GoBackNConn) Send(data []byte) error {
174154 default :
175155 }
176156
177- g .sendTimeoutMu .RLock ()
178- ticker := time .NewTimer (g .sendTimeout )
179- g .sendTimeoutMu .RUnlock ()
157+ ticker := time .NewTimer (g .timeoutManager .GetSendTimeout ())
180158 defer ticker .Stop ()
181159
182160 sendPacket := func (packet * PacketData ) error {
@@ -235,9 +213,7 @@ func (g *GoBackNConn) Recv() ([]byte, error) {
235213 msg * PacketData
236214 )
237215
238- g .recvTimeoutMu .RLock ()
239- ticker := time .NewTimer (g .recvTimeout )
240- g .recvTimeoutMu .RUnlock ()
216+ ticker := time .NewTimer (g .timeoutManager .GetRecvTimeout ())
241217 defer ticker .Stop ()
242218
243219 for {
@@ -279,7 +255,7 @@ func (g *GoBackNConn) start() {
279255
280256 g .pongTicker = NewIntervalAwareForceTicker (pongTime )
281257
282- g .resendTicker = time .NewTicker (g .resendTimeout )
258+ g .resendTicker = time .NewTicker (g .timeoutManager . GetResendTimeout () )
283259
284260 g .wg .Add (1 )
285261 go func () {
@@ -335,7 +311,7 @@ func (g *GoBackNConn) Close() error {
335311 default :
336312 log .Tracef ("Try sending FIN, isServer=%v" , g .isServer )
337313 ctxc , cancel := context .WithTimeout (
338- g .ctx , finSendTimeout ,
314+ g .ctx , g . timeoutManager . GetFinSendTimeout () ,
339315 )
340316 defer cancel ()
341317 if err := g .sendPacket (ctxc , & PacketFIN {}); err != nil {
@@ -388,7 +364,7 @@ func (g *GoBackNConn) sendPacketsForever() error {
388364 // resendQueue re-sends the current contents of the queue.
389365 resendQueue := func () error {
390366 err := g .sendQueue .resend (
391- g .resendTimeout , g . quit ,
367+ g .quit ,
392368 func (packet * PacketData ) error {
393369 return g .sendPacket (g .ctx , packet )
394370 },
@@ -400,7 +376,7 @@ func (g *GoBackNConn) sendPacketsForever() error {
400376 // execute. That can happen if the function was awaiting the
401377 // expected ACK for a long time, or times out while awaiting the
402378 // catch up.
403- g .resendTicker .Reset (g .resendTimeout )
379+ g .resendTicker .Reset (g .timeoutManager . GetResendTimeout () )
404380
405381 return err
406382 }
@@ -520,7 +496,7 @@ func (g *GoBackNConn) receivePacketsForever() error { // nolint:gocyclo
520496 g .pongTicker .Pause ()
521497 }
522498
523- g .resendTicker .Reset (g .resendTimeout )
499+ g .resendTicker .Reset (g .timeoutManager . GetResendTimeout () )
524500
525501 switch m := msg .(type ) {
526502 case * PacketData :
@@ -578,7 +554,9 @@ func (g *GoBackNConn) receivePacketsForever() error { // nolint:gocyclo
578554 // the resend, and therefore won't react to the
579555 // NACK we send here in time.
580556 sinceSent := time .Since (lastNackTime )
581- recentlySent := sinceSent < g .resendTimeout * 2 //nolint:gomnd
557+
558+ timeout := g .timeoutManager .GetResendTimeout ()
559+ recentlySent := sinceSent < timeout * 2 //nolint:gomnd
582560
583561 if lastNackSeq == g .recvSeq && recentlySent {
584562 log .Tracef ("Recently sent NACK" )
@@ -603,9 +581,7 @@ func (g *GoBackNConn) receivePacketsForever() error { // nolint:gocyclo
603581 }
604582
605583 case * PacketACK :
606- gotValidACK := g .sendQueue .processACK (
607- m .Seq , g .resendTimeout ,
608- )
584+ gotValidACK := g .sendQueue .processACK (m .Seq )
609585
610586 if gotValidACK {
611587 // Send a signal to indicate that new
0 commit comments