Skip to content

Commit 760dc28

Browse files
committed
Move saving of connections to those that work.
Also amends the idle timeout test to run faster.
1 parent 36b7b46 commit 760dc28

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

Diff for: packet.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (pt packetType) isProtected() bool {
8080

8181
// kCidDefaultLength is the length of connection ID we generate.
8282
// TODO: make this configurable.
83-
const kCidDefaultLength = 8
83+
const kCidDefaultLength = 5
8484

8585
// ConnectionId identifies the connection that a packet belongs to.
8686
type ConnectionId []byte

Diff for: server.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,24 @@ func (s *Server) Input(addr *net.UDPAddr, data []byte) (*Connection, error) {
6767
}
6868
conn = NewConnection(trans, RoleServer, s.tls, nil)
6969
newConn = true
70-
s.idTable[conn.serverConnectionId.String()] = conn
71-
s.addrTable[addr.String()] = conn
7270
}
7371

7472
err = conn.Input(data)
7573
if isFatalError(err) {
7674
logf(logTypeServer, "Fatal Error %v killing connection %v", err, conn)
77-
delete(s.idTable, conn.serverConnectionId.String())
78-
delete(s.addrTable, addr.String())
7975
return nil, nil
8076
}
8177

82-
if newConn && s.handler != nil {
83-
s.handler.NewConnection(conn)
78+
if newConn {
79+
// Wait until handling the first packet before the connection is added
80+
// to the table. Firstly, to avoid having to remove it if there is an
81+
// error, but also because the server-chosen connection ID isn't set
82+
// until after the Initial is handled.
83+
s.idTable[conn.serverConnectionId.String()] = conn
84+
s.addrTable[addr.String()] = conn
85+
if s.handler != nil {
86+
s.handler.NewConnection(conn)
87+
}
8488
}
8589

8690
return conn, nil

Diff for: server_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,19 @@ func TestServerIdleTimeout(t *testing.T) {
110110
assertEquals(t, 1, n)
111111
assertNotError(t, err, "Couldn't send client initial")
112112

113-
_, err = serverInputAll(t, sTrans, server, *u)
113+
sconn, err := serverInputAll(t, sTrans, server, *u)
114114
assertNotError(t, err, "Couldn't consume client initial")
115+
assertNotNil(t, sconn, "no server connection")
115116

116117
assertEquals(t, 1, server.ConnectionCount())
117118

118-
// Now wait 6 seconds to make sure that the connection
119-
// gets garbage collected.
120-
121-
time.Sleep(time.Second * 6)
119+
// This pokes into internal state of the server to avoid having to include
120+
// sleep calls in tests. Don't do this at home kids.
121+
// Wind the timer on the connection back to short-circuit the idle timeout.
122+
sconn.lastInput = sconn.lastInput.Add(-1 - sconn.idleTimeout)
122123
server.CheckTimer()
123124
// A second nap to allow for draining period.
124-
time.Sleep(time.Second)
125+
sconn.closingEnd = sconn.closingEnd.Add(-1 - time.Second)
125126
server.CheckTimer()
126127

127128
assertEquals(t, 0, server.ConnectionCount())

0 commit comments

Comments
 (0)