Skip to content

Commit fa60171

Browse files
authored
Merge pull request #90 from lightninglabs/gbnCleanup
gbn+mailbox: cleanup & prefixed logger
2 parents 9cf86bc + 72e5adf commit fa60171

File tree

12 files changed

+366
-242
lines changed

12 files changed

+366
-242
lines changed

gbn/config.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package gbn
2+
3+
import "time"
4+
5+
// config holds the configuration values for an instance of GoBackNConn.
6+
type config struct {
7+
// n is the window size. The sender can send a maximum of n packets
8+
// before requiring an ack from the receiver for the first packet in
9+
// the window. The value of n is chosen by the client during the
10+
// GoBN handshake.
11+
n uint8
12+
13+
// s is the maximum sequence number used to label packets. Packets
14+
// are labelled with incrementing sequence numbers modulo s.
15+
// s must be strictly larger than the window size, n. This
16+
// is so that the receiver can tell if the sender is resending the
17+
// previous window (maybe the sender did not receive the acks) or if
18+
// they are sending the next window. If s <= n then there would be
19+
// no way to tell.
20+
s uint8
21+
22+
// maxChunkSize is the maximum payload size in bytes allowed per
23+
// message. If the payload to be sent is larger than maxChunkSize then
24+
// the payload will be split between multiple packets.
25+
// If maxChunkSize is zero then it is disabled and data won't be split
26+
// between packets.
27+
maxChunkSize int
28+
29+
// resendTimeout is the duration that will be waited before resending
30+
// the packets in the current queue.
31+
resendTimeout time.Duration
32+
33+
// recvFromStream is the function that will be used to acquire the next
34+
// available packet.
35+
recvFromStream recvBytesFunc
36+
37+
// sendToStream is the function that will be used to send over our next
38+
// packet.
39+
sendToStream sendBytesFunc
40+
41+
// handshakeTimeout is the time after which the server or client
42+
// will abort and restart the handshake if the expected response is
43+
// not received from the peer.
44+
handshakeTimeout time.Duration
45+
46+
pingTime time.Duration
47+
pongTime time.Duration
48+
}
49+
50+
// newConfig constructs a new config struct.
51+
func newConfig(sendFunc sendBytesFunc, recvFunc recvBytesFunc,
52+
n uint8) *config {
53+
54+
return &config{
55+
n: n,
56+
s: n + 1,
57+
recvFromStream: recvFunc,
58+
sendToStream: sendFunc,
59+
resendTimeout: defaultResendTimeout,
60+
handshakeTimeout: defaultHandshakeTimeout,
61+
}
62+
}

gbn/gbn_client.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ func NewClientConn(ctx context.Context, n uint8, sendFunc sendBytesFunc,
2121
math.MaxUint8)
2222
}
2323

24-
conn := newGoBackNConn(ctx, sendFunc, receiveFunc, false, n)
24+
cfg := newConfig(sendFunc, receiveFunc, n)
2525

2626
// Apply functional options
2727
for _, o := range opts {
28-
o(conn)
28+
o(cfg)
2929
}
3030

31+
conn := newGoBackNConn(ctx, cfg, "client")
32+
3133
if err := conn.clientHandshake(); err != nil {
3234
if err := conn.Close(); err != nil {
33-
log.Errorf("error closing gbn ClientConn: %v", err)
35+
conn.log.Errorf("error closing gbn ClientConn: %v", err)
3436
}
3537
return nil, err
3638
}
@@ -76,7 +78,7 @@ func (g *GoBackNConn) clientHandshake() error {
7678
case <-recvNext:
7779
}
7880

79-
b, err := g.recvFromStream(g.ctx)
81+
b, err := g.cfg.recvFromStream(g.ctx)
8082
if err != nil {
8183
errChan <- err
8284
return
@@ -101,21 +103,22 @@ func (g *GoBackNConn) clientHandshake() error {
101103
handshake:
102104
for {
103105
// start Handshake
104-
msg := &PacketSYN{N: g.n}
106+
msg := &PacketSYN{N: g.cfg.n}
105107
msgBytes, err := msg.Serialize()
106108
if err != nil {
107109
return err
108110
}
109111

110112
// Send SYN
111-
log.Debugf("Client sending SYN")
112-
if err := g.sendToStream(g.ctx, msgBytes); err != nil {
113+
g.log.Debugf("Sending SYN")
114+
if err := g.cfg.sendToStream(g.ctx, msgBytes); err != nil {
113115
return err
114116
}
115117

116118
for {
117119
// Wait for SYN
118-
log.Debugf("Client waiting for SYN")
120+
g.log.Debugf("Waiting for SYN")
121+
119122
select {
120123
case recvNext <- 1:
121124
case <-g.quit:
@@ -127,8 +130,10 @@ handshake:
127130

128131
var b []byte
129132
select {
130-
case <-time.After(g.handshakeTimeout):
131-
log.Debugf("SYN resendTimeout. Resending SYN.")
133+
case <-time.After(g.cfg.handshakeTimeout):
134+
g.log.Debugf("SYN resendTimeout. Resending " +
135+
"SYN.")
136+
132137
continue handshake
133138
case <-g.quit:
134139
return nil
@@ -144,7 +149,8 @@ handshake:
144149
return err
145150
}
146151

147-
log.Debugf("Client got %T", resp)
152+
g.log.Debugf("Got %T", resp)
153+
148154
switch r := resp.(type) {
149155
case *PacketSYN:
150156
respSYN = r
@@ -159,24 +165,24 @@ handshake:
159165
}
160166
}
161167

162-
log.Debugf("Client got SYN")
168+
g.log.Debugf("Got SYN")
163169

164-
if respSYN.N != g.n {
170+
if respSYN.N != g.cfg.n {
165171
return io.EOF
166172
}
167173

168174
// Send SYNACK
169-
log.Debugf("Client sending SYNACK")
175+
g.log.Debugf("Sending SYNACK")
170176
synack, err := new(PacketSYNACK).Serialize()
171177
if err != nil {
172178
return err
173179
}
174180

175-
if err := g.sendToStream(g.ctx, synack); err != nil {
181+
if err := g.cfg.sendToStream(g.ctx, synack); err != nil {
176182
return err
177183
}
178184

179-
log.Debugf("Client Handshake complete")
185+
g.log.Debugf("Handshake complete")
180186

181187
return nil
182188
}

0 commit comments

Comments
 (0)