Skip to content

Commit 17b8366

Browse files
gbn + mailbox: modify TimeoutManager through conf
This commit changes the gbn `Config` struct to not specifically contain any timeout fields, but instead contain functional options that modifies the `TimeoutManager` struct. These options are then applied to the `TimeoutManager`.
1 parent 50db412 commit 17b8366

File tree

5 files changed

+26
-58
lines changed

5 files changed

+26
-58
lines changed

gbn/config.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ func WithTimeoutUpdateFrequency(frequency int) TimeoutOptions {
4444
}
4545
}
4646

47-
// WithTMHandshakeTimeout is used to set the timeout used during the handshake.
47+
// WithHandshakeTimeout is used to set the timeout used during the handshake.
4848
// If the timeout is reached without response from the peer then the handshake
4949
// will be aborted and restarted.
50-
func WithTMHandshakeTimeout(timeout time.Duration) TimeoutOptions {
50+
func WithHandshakeTimeout(timeout time.Duration) TimeoutOptions {
5151
return func(manager *TimeoutManager) {
5252
manager.handshakeTimeout = timeout
5353
}
5454
}
5555

56-
// WithTMKeepalivePing is used to send a ping packet if no packets have been
56+
// WithKeepalivePing is used to send a ping packet if no packets have been
5757
// received from the other side for the given duration. This helps keep the
5858
// connection alive and also ensures that the connection is closed if the
5959
// other side does not respond to the ping in a timely manner. After the ping
6060
// the connection will be closed if the other side does not respond within
6161
// time duration.
62-
func WithTMKeepalivePing(ping, pong time.Duration) TimeoutOptions {
62+
func WithKeepalivePing(ping, pong time.Duration) TimeoutOptions {
6363
return func(manager *TimeoutManager) {
6464
manager.pingTime = ping
6565
manager.pongTime = pong
@@ -90,10 +90,6 @@ type config struct {
9090
// between packets.
9191
maxChunkSize int
9292

93-
// resendTimeout is the duration that will be waited before resending
94-
// the packets in the current queue.
95-
resendTimeout time.Duration
96-
9793
// recvFromStream is the function that will be used to acquire the next
9894
// available packet.
9995
recvFromStream recvBytesFunc
@@ -106,25 +102,17 @@ type config struct {
106102
// been received and processed.
107103
onFIN func()
108104

109-
// handshakeTimeout is the time after which the server or client
110-
// will abort and restart the handshake if the expected response is
111-
// not received from the peer.
112-
handshakeTimeout time.Duration
113-
114-
pingTime time.Duration
115-
pongTime time.Duration
105+
timeoutOptions []TimeoutOptions
116106
}
117107

118108
// newConfig constructs a new config struct.
119109
func newConfig(sendFunc sendBytesFunc, recvFunc recvBytesFunc,
120110
n uint8) *config {
121111

122112
return &config{
123-
n: n,
124-
s: n + 1,
125-
recvFromStream: recvFunc,
126-
sendToStream: sendFunc,
127-
resendTimeout: defaultResendTimeout,
128-
handshakeTimeout: defaultHandshakeTimeout,
113+
n: n,
114+
s: n + 1,
115+
recvFromStream: recvFunc,
116+
sendToStream: sendFunc,
129117
}
130118
}

gbn/gbn_conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func newGoBackNConn(ctx context.Context, cfg *config,
8383
prefix := fmt.Sprintf("(%s)", loggerPrefix)
8484
plog := build.NewPrefixLog(prefix, log)
8585

86-
timeoutManager := NewTimeOutManager(plog)
86+
timeoutManager := NewTimeOutManager(plog, cfg.timeoutOptions...)
8787

8888
g := &GoBackNConn{
8989
cfg: cfg,

gbn/options.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package gbn
22

3-
import "time"
4-
53
type Option func(conn *config)
64

75
// WithMaxSendSize is used to set the maximum payload size in bytes per packet.
@@ -14,33 +12,11 @@ func WithMaxSendSize(size int) Option {
1412
}
1513
}
1614

17-
// WithTimeout is used to set the resend timeout. This is the time to wait
18-
// for ACKs before resending the queue.
19-
func WithTimeout(timeout time.Duration) Option {
20-
return func(conn *config) {
21-
conn.resendTimeout = timeout
22-
}
23-
}
24-
25-
// WithHandshakeTimeout is used to set the timeout used during the handshake.
26-
// If the timeout is reached without response from the peer then the handshake
27-
// will be aborted and restarted.
28-
func WithHandshakeTimeout(timeout time.Duration) Option {
29-
return func(conn *config) {
30-
conn.handshakeTimeout = timeout
31-
}
32-
}
33-
34-
// WithKeepalivePing is used to send a ping packet if no packets have been
35-
// received from the other side for the given duration. This helps keep the
36-
// connection alive and also ensures that the connection is closed if the
37-
// other side does not respond to the ping in a timely manner. After the ping
38-
// the connection will be closed if the other side does not respond within
39-
// time duration.
40-
func WithKeepalivePing(ping, pong time.Duration) Option {
15+
// WithTimeoutOptions is used to set the different timeout options that will be
16+
// used within gbn package.
17+
func WithTimeoutOptions(opts ...TimeoutOptions) Option {
4118
return func(conn *config) {
42-
conn.pingTime = ping
43-
conn.pongTime = pong
19+
conn.timeoutOptions = opts
4420
}
4521
}
4622

mailbox/client_conn.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,12 @@ func NewClientConn(ctx context.Context, sid [64]byte, serverHost string,
166166
}
167167

168168
c.gbnOptions = []gbn.Option{
169-
gbn.WithTimeout(gbnTimeout),
170-
gbn.WithHandshakeTimeout(gbnHandshakeTimeout),
171-
gbn.WithKeepalivePing(
172-
gbnClientPingTimeout, gbnPongTimeout,
169+
gbn.WithTimeoutOptions(
170+
gbn.WithStaticResendTimeout(gbnTimeout),
171+
gbn.WithHandshakeTimeout(gbnHandshakeTimeout),
172+
gbn.WithKeepalivePing(
173+
gbnClientPingTimeout, gbnPongTimeout,
174+
),
173175
),
174176
gbn.WithOnFIN(func() {
175177
// We force the connection to set a new status after

mailbox/server_conn.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ func NewServerConn(ctx context.Context, serverHost string,
8080
cancel: cancel,
8181
quit: make(chan struct{}),
8282
gbnOptions: []gbn.Option{
83-
gbn.WithTimeout(gbnTimeout),
84-
gbn.WithHandshakeTimeout(gbnHandshakeTimeout),
85-
gbn.WithKeepalivePing(
86-
gbnServerPingTimeout, gbnPongTimeout,
83+
gbn.WithTimeoutOptions(
84+
gbn.WithStaticResendTimeout(gbnTimeout),
85+
gbn.WithHandshakeTimeout(gbnHandshakeTimeout),
86+
gbn.WithKeepalivePing(
87+
gbnServerPingTimeout, gbnPongTimeout,
88+
),
8789
),
8890
},
8991
status: ServerStatusNotConnected,

0 commit comments

Comments
 (0)