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 {}
@@ -103,6 +84,10 @@ type GoBackNConn struct {
10384 // remoteClosed is closed if the remote party initiated the FIN sequence.
10485 remoteClosed chan struct {}
10586
87+ // timeoutManager is used to manage all the timeouts used by the
88+ // GoBackNConn.
89+ timeoutManager * TimeoutManager
90+
10691 // quit is used to stop the normal operations of the connection.
10792 // Once closed, the send and receive streams will still be available
10893 // for the FIN sequence.
@@ -121,21 +106,20 @@ func newGoBackNConn(ctx context.Context, sendFunc sendBytesFunc,
121106
122107 ctxc , cancel := context .WithCancel (ctx )
123108
109+ timeoutManager := NewTimeOutManager (isServer )
110+
124111 gbn := & GoBackNConn {
125- resendTimeout : defaultResendTimeout ,
126112 recvFromStream : recvFunc ,
127113 sendToStream : sendFunc ,
128114 sendDataChan : make (chan * PacketData ),
129115 isServer : isServer ,
130- handshakeTimeout : defaultHandshakeTimeout ,
131- recvTimeout : DefaultRecvTimeout ,
132- sendTimeout : DefaultSendTimeout ,
133116 receivedACKSignal : make (chan struct {}),
134117 resendSignal : make (chan struct {}, 1 ),
135118 remoteClosed : make (chan struct {}),
136119 ctx : ctxc ,
137120 cancel : cancel ,
138121 quit : make (chan struct {}),
122+ timeoutManager : timeoutManager ,
139123 }
140124
141125 for _ , o := range opts {
@@ -147,34 +131,31 @@ func newGoBackNConn(ctx context.Context, sendFunc sendBytesFunc,
147131 return gbn
148132}
149133
134+ // SetSendTimeout sets the timeout used in the Send function.
135+ func (g * GoBackNConn ) SetSendTimeout (timeout time.Duration ) {
136+ g .timeoutManager .SetSendTimeout (timeout )
137+ }
138+
139+ // SetRecvTimeout sets the timeout used in the Recv function.
140+ func (g * GoBackNConn ) SetRecvTimeout (timeout time.Duration ) {
141+ g .timeoutManager .SetRecvTimeout (timeout )
142+ }
143+
150144// setN sets the current N to use. This _must_ be set before the handshake is
151145// completed.
152146func (g * GoBackNConn ) setN (n uint8 ) {
153147 g .n = n
154148 g .s = n + 1
155149 g .recvDataChan = make (chan * PacketData , n )
156- g .sendQueue = newQueue (& queueConfig {
150+
151+ cfg := & queueConfig {
157152 s : g .s ,
158153 sendPkt : func (packet * PacketData ) error {
159154 return g .sendPacket (g .ctx , packet )
160155 },
161- })
162- }
163-
164- // SetSendTimeout sets the timeout used in the Send function.
165- func (g * GoBackNConn ) SetSendTimeout (timeout time.Duration ) {
166- g .sendTimeoutMu .Lock ()
167- defer g .sendTimeoutMu .Unlock ()
168-
169- g .sendTimeout = timeout
170- }
171-
172- // SetRecvTimeout sets the timeout used in the Recv function.
173- func (g * GoBackNConn ) SetRecvTimeout (timeout time.Duration ) {
174- g .recvTimeoutMu .Lock ()
175- defer g .recvTimeoutMu .Unlock ()
156+ }
176157
177- g .recvTimeout = timeout
158+ g .sendQueue = newQueue ( cfg , g . timeoutManager )
178159}
179160
180161// Send blocks until an ack is received for the packet sent N packets before.
@@ -186,9 +167,7 @@ func (g *GoBackNConn) Send(data []byte) error {
186167 default :
187168 }
188169
189- g .sendTimeoutMu .RLock ()
190- ticker := time .NewTimer (g .sendTimeout )
191- g .sendTimeoutMu .RUnlock ()
170+ ticker := time .NewTimer (g .timeoutManager .GetSendTimeout ())
192171 defer ticker .Stop ()
193172
194173 sendPacket := func (packet * PacketData ) error {
@@ -247,9 +226,7 @@ func (g *GoBackNConn) Recv() ([]byte, error) {
247226 msg * PacketData
248227 )
249228
250- g .recvTimeoutMu .RLock ()
251- ticker := time .NewTimer (g .recvTimeout )
252- g .recvTimeoutMu .RUnlock ()
229+ ticker := time .NewTimer (g .timeoutManager .GetRecvTimeout ())
253230 defer ticker .Stop ()
254231
255232 for {
@@ -291,7 +268,7 @@ func (g *GoBackNConn) start() {
291268
292269 g .pongTicker = NewIntervalAwareForceTicker (pongTime )
293270
294- g .resendTicker = time .NewTicker (g .resendTimeout )
271+ g .resendTicker = time .NewTicker (g .timeoutManager . GetResendTimeout () )
295272
296273 g .wg .Add (1 )
297274 go func () {
@@ -347,7 +324,7 @@ func (g *GoBackNConn) Close() error {
347324 default :
348325 log .Tracef ("Try sending FIN, isServer=%v" , g .isServer )
349326 ctxc , cancel := context .WithTimeout (
350- g .ctx , finSendTimeout ,
327+ g .ctx , g . timeoutManager . GetFinSendTimeout () ,
351328 )
352329 defer cancel ()
353330 if err := g .sendPacket (ctxc , & PacketFIN {}); err != nil {
@@ -401,15 +378,15 @@ func (g *GoBackNConn) sendPacket(ctx context.Context, msg Message) error {
401378func (g * GoBackNConn ) sendPacketsForever () error {
402379 // resendQueue re-sends the current contents of the queue.
403380 resendQueue := func () error {
404- err := g .sendQueue .resend (g . resendTimeout )
381+ err := g .sendQueue .resend ()
405382
406383 // After resending the queue, we reset the resend ticker.
407384 // This is so that we don't immediately resend the queue again,
408385 // if the sendQueue.resend call above took a long time to
409386 // execute. That can happen if the function was awaiting the
410387 // expected ACK for a long time, or times out while awaiting the
411388 // catch up.
412- g .resendTicker .Reset (g .resendTimeout )
389+ g .resendTicker .Reset (g .timeoutManager . GetResendTimeout () )
413390
414391 return err
415392 }
@@ -529,7 +506,7 @@ func (g *GoBackNConn) receivePacketsForever() error { // nolint:gocyclo
529506 g .pongTicker .Pause ()
530507 }
531508
532- g .resendTicker .Reset (g .resendTimeout )
509+ g .resendTicker .Reset (g .timeoutManager . GetResendTimeout () )
533510
534511 switch m := msg .(type ) {
535512 case * PacketData :
@@ -587,7 +564,9 @@ func (g *GoBackNConn) receivePacketsForever() error { // nolint:gocyclo
587564 // the resend, and therefore won't react to the
588565 // NACK we send here in time.
589566 sinceSent := time .Since (lastNackTime )
590- recentlySent := sinceSent < g .resendTimeout * 2 //nolint:gomnd
567+
568+ timeout := g .timeoutManager .GetResendTimeout ()
569+ recentlySent := sinceSent < timeout * 2 //nolint:gomnd
591570
592571 if lastNackSeq == g .recvSeq && recentlySent {
593572 log .Tracef ("Recently sent NACK" )
@@ -612,9 +591,7 @@ func (g *GoBackNConn) receivePacketsForever() error { // nolint:gocyclo
612591 }
613592
614593 case * PacketACK :
615- gotValidACK := g .sendQueue .processACK (
616- m .Seq , g .resendTimeout ,
617- )
594+ gotValidACK := g .sendQueue .processACK (m .Seq )
618595
619596 if gotValidACK {
620597 // Send a signal to indicate that new
0 commit comments