Skip to content

Commit 6000041

Browse files
gbn: add timeout manager
The timeout manager is a manager for all timeout values used by the different components of the gbn package. The timeout manager enables the functionality of dynamically changing the resend timeout value based on how long it takes to receive an response from the counterparty. To make the gbn package more modular, we'll move the responsibility of managing the timeout values of the gbn package to the timeout manager in the next commit.
1 parent 445d935 commit 6000041

File tree

4 files changed

+684
-7
lines changed

4 files changed

+684
-7
lines changed

gbn/config.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,70 @@ package gbn
22

33
import "time"
44

5+
// TimeoutOptions can be used to modify the default timeout values used within
6+
// the TimeoutManager.
7+
type TimeoutOptions func(manager *TimeoutManager)
8+
9+
// WithStaticResendTimeout is used to set a static resend timeout. This is the
10+
// time to wait for ACKs before resending the queue.
11+
func WithStaticResendTimeout(timeout time.Duration) TimeoutOptions {
12+
return func(manager *TimeoutManager) {
13+
manager.useStaticTimeout = true
14+
manager.resendTimeout = timeout
15+
}
16+
}
17+
18+
// WithResendMultiplier is used to set the resend multiplier. This is the
19+
// multiplier we use when dynamically setting the resend timeout, based on how
20+
// long it took for other party to respond.
21+
// Note that when setting the resend timeout manually with the
22+
// WithStaticResendTimeout option, this option will have no effect.
23+
// Note that the passed multiplier must be greater than zero or this option will
24+
// have no effect.
25+
func WithResendMultiplier(multiplier int) TimeoutOptions {
26+
return func(manager *TimeoutManager) {
27+
if multiplier > 0 {
28+
manager.resendMultiplier = multiplier
29+
}
30+
}
31+
}
32+
33+
// WithTimeoutUpdateFrequency is used to set the frequency of how many
34+
// corresponding responses we need to receive until updating the resend timeout.
35+
// Note that when setting the resend timeout manually with the WithTimeout
36+
// option, this option will have no effect.
37+
// Also note that the passed frequency must be greater than zero or this option
38+
// will have no effect.
39+
func WithTimeoutUpdateFrequency(frequency int) TimeoutOptions {
40+
return func(manager *TimeoutManager) {
41+
if frequency > 0 {
42+
manager.timeoutUpdateFrequency = frequency
43+
}
44+
}
45+
}
46+
47+
// WithTMHandshakeTimeout is used to set the timeout used during the handshake.
48+
// If the timeout is reached without response from the peer then the handshake
49+
// will be aborted and restarted.
50+
func WithTMHandshakeTimeout(timeout time.Duration) TimeoutOptions {
51+
return func(manager *TimeoutManager) {
52+
manager.handshakeTimeout = timeout
53+
}
54+
}
55+
56+
// WithTMKeepalivePing is used to send a ping packet if no packets have been
57+
// received from the other side for the given duration. This helps keep the
58+
// connection alive and also ensures that the connection is closed if the
59+
// other side does not respond to the ping in a timely manner. After the ping
60+
// the connection will be closed if the other side does not respond within
61+
// time duration.
62+
func WithTMKeepalivePing(ping, pong time.Duration) TimeoutOptions {
63+
return func(manager *TimeoutManager) {
64+
manager.pingTime = ping
65+
manager.pongTime = pong
66+
}
67+
}
68+
569
// config holds the configuration values for an instance of GoBackNConn.
670
type config struct {
771
// n is the window size. The sender can send a maximum of n packets

gbn/gbn_conn.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ var (
2121
)
2222

2323
const (
24-
DefaultN = 20
25-
defaultHandshakeTimeout = 100 * time.Millisecond
26-
defaultResendTimeout = 100 * time.Millisecond
27-
finSendTimeout = 1000 * time.Millisecond
28-
DefaultSendTimeout = math.MaxInt64
29-
DefaultRecvTimeout = math.MaxInt64
24+
DefaultN = 20
3025
)
3126

3227
type sendBytesFunc func(ctx context.Context, b []byte) error
@@ -322,7 +317,7 @@ func (g *GoBackNConn) Close() error {
322317
g.log.Tracef("Try sending FIN")
323318

324319
ctxc, cancel := context.WithTimeout(
325-
g.ctx, finSendTimeout,
320+
g.ctx, defaultFinSendTimeout,
326321
)
327322
defer cancel()
328323
if err := g.sendPacket(ctxc, &PacketFIN{}); err != nil {

0 commit comments

Comments
 (0)