aboutsummaryrefslogtreecommitdiff
path: root/internal/net
diff options
context:
space:
mode:
Diffstat (limited to 'internal/net')
-rw-r--r--internal/net/conn.go8
-rw-r--r--internal/net/remote_host_test.go1
-rw-r--r--internal/net/server.go29
-rw-r--r--internal/net/server_test.go1
-rw-r--r--internal/net/wsconn.go5
5 files changed, 32 insertions, 12 deletions
diff --git a/internal/net/conn.go b/internal/net/conn.go
index ca40354..a4be83a 100644
--- a/internal/net/conn.go
+++ b/internal/net/conn.go
@@ -23,6 +23,10 @@ type Conn interface {
// instead of a hung writer goroutine. Implementations without deadline
// support return nil (deadlines are best-effort).
SetWriteDeadline(t time.Time) error
+ // SetReadDeadline is the read-side counterpart: transports implement it so
+ // a silent peer cannot pin a goroutine and a connection slot forever.
+ // Implementations without deadline support return nil.
+ SetReadDeadline(t time.Time) error
SetEcho(bool) error
}
@@ -114,6 +118,10 @@ func (t *tcpConn) SetWriteDeadline(ts time.Time) error {
return t.conn.SetWriteDeadline(ts)
}
+func (t *tcpConn) SetReadDeadline(ts time.Time) error {
+ return t.conn.SetReadDeadline(ts)
+}
+
func (t *tcpConn) Close() error {
return t.conn.Close()
}
diff --git a/internal/net/remote_host_test.go b/internal/net/remote_host_test.go
index 690e758..bfd10f5 100644
--- a/internal/net/remote_host_test.go
+++ b/internal/net/remote_host_test.go
@@ -13,6 +13,7 @@ func (c remoteAddrConn) ReadMessage() (string, error) { return "", nil }
func (c remoteAddrConn) Write(b []byte) (int, error) { return len(b), nil }
func (c remoteAddrConn) Close() error { return nil }
func (c remoteAddrConn) SetWriteDeadline(time.Time) error { return nil }
+func (c remoteAddrConn) SetReadDeadline(time.Time) error { return nil }
func (c remoteAddrConn) SetEcho(bool) error { return nil }
func (c remoteAddrConn) RemoteAddr() string { return c.addr }
diff --git a/internal/net/server.go b/internal/net/server.go
index 6367419..71e34c8 100644
--- a/internal/net/server.go
+++ b/internal/net/server.go
@@ -331,17 +331,11 @@ func sessionIdleTimeout(sess *Session) time.Duration {
}
}
-// armReadDeadline sets an idle read deadline on the session's transport when
-// it supports one. tcpConn and wsConn (both in this package) expose their
-// underlying connection, so telnet, telnet-TLS, and websocket are all covered.
+// armReadDeadline sets an idle read deadline on the session's transport. The
+// Conn interface requires SetReadDeadline, so every transport (telnet,
+// telnet-TLS, websocket) is covered and a new one cannot silently miss it.
func armReadDeadline(sess *Session, d time.Duration) {
- deadline := time.Now().Add(d)
- switch c := sess.Conn.(type) {
- case *tcpConn:
- _ = c.conn.SetReadDeadline(deadline)
- case *wsConn:
- _ = c.conn.SetReadDeadline(deadline)
- }
+ _ = sess.Conn.SetReadDeadline(time.Now().Add(d))
}
// StartWriter puts the session into queued mode: writes are enqueued and a
@@ -559,8 +553,11 @@ func (h *Hub) LeaveRoom(s *Session) {
}
func (h *Hub) leaveRoomLocked(s *Session) {
- for _, room := range h.rooms {
+ for id, room := range h.rooms {
delete(room, s)
+ if len(room) == 0 {
+ delete(h.rooms, id)
+ }
}
}
@@ -582,7 +579,15 @@ func (h *Hub) RekeyRooms(idMap map[int]int) {
}
}
for newID, set := range moved {
- h.rooms[newID] = set
+ if existing := h.rooms[newID]; existing != nil {
+ // Merge instead of clobbering a target bucket that exists but is
+ // not itself in idMap; assigning would drop its sessions.
+ for s := range set {
+ existing[s] = true
+ }
+ } else {
+ h.rooms[newID] = set
+ }
}
}
diff --git a/internal/net/server_test.go b/internal/net/server_test.go
index 6a3b376..b540704 100644
--- a/internal/net/server_test.go
+++ b/internal/net/server_test.go
@@ -47,6 +47,7 @@ type testConn struct{ data []byte }
func (c *testConn) ReadMessage() (string, error) { return "", nil }
func (c *testConn) Write(b []byte) (int, error) { c.data = append(c.data, b...); return len(b), nil }
func (c *testConn) SetWriteDeadline(time.Time) error { return nil }
+func (c *testConn) SetReadDeadline(time.Time) error { return nil }
func (c *testConn) Close() error { return nil }
func (c *testConn) SetEcho(bool) error { return nil }
diff --git a/internal/net/wsconn.go b/internal/net/wsconn.go
index 95f714c..e7763aa 100644
--- a/internal/net/wsconn.go
+++ b/internal/net/wsconn.go
@@ -41,6 +41,11 @@ func (w *wsConn) SetWriteDeadline(ts time.Time) error {
return w.conn.SetWriteDeadline(ts)
}
+// SetReadDeadline delegates to the underlying websocket connection.
+func (w *wsConn) SetReadDeadline(ts time.Time) error {
+ return w.conn.SetReadDeadline(ts)
+}
+
func (w *wsConn) Close() error {
return w.conn.Close()
}