diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-06-25 07:44:23 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-06-25 07:44:23 -0700 |
| commit | bba70d0a2903ab1fff5a2e89cff97cb1f6c7f82a (patch) | |
| tree | cb8bd5cda76f287358bd7b73292ae024e756c824 /hotline/server.go | |
| parent | 4912c3a7c20d765692993f6caaa98a94f5ae28cb (diff) | |
Introduce PresenceTracker interface for online-user tracking
Replace the leaky *redis.Client field on Server with a PresenceTracker
interface that receives connect/rename/disconnect lifecycle events. This
removes Redis-specific set encoding from hotline/server.go and the session
handlers, and drops the redis dependency from the hotline package.
The Redis implementation moves to mobius.RedisPresenceTracker, which owns
the legacy "login::ip"/"login:nickname:ip" set encoding so existing
deployments keep working. The API server reads online users through a new
OnlineLister interface and falls back to the in-memory ClientMgr when no
presence tracker is configured. Startup clearing of stale online state now
happens unconditionally when Redis is configured, not only when the API
server is enabled.
Diffstat (limited to 'hotline/server.go')
| -rw-r--r-- | hotline/server.go | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/hotline/server.go b/hotline/server.go index 8a530c2..020f878 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -16,7 +16,6 @@ import ( "sync" "time" - "github.com/redis/go-redis/v9" "golang.org/x/text/encoding" "golang.org/x/text/encoding/charmap" "golang.org/x/time/rate" @@ -62,7 +61,9 @@ type Server struct { MessageBoard io.ReadWriteSeeker - Redis *redis.Client + // Presence optionally records user session lifecycle events for an external online-user + // list. When nil, online presence is derived from ClientMgr. + Presence PresenceTracker // TrackerRegistrar handles tracker registration (injectable for testing) TrackerRegistrar TrackerRegistrar @@ -133,6 +134,13 @@ func WithTrackerRegistrar(registrar TrackerRegistrar) func(s *Server) { } } +// WithPresenceTracker optionally sets a PresenceTracker to record user session lifecycle events. +func WithPresenceTracker(p PresenceTracker) func(s *Server) { + return func(s *Server) { + s.Presence = p + } +} + // WithTLS optionally enables TLS support on the specified port. func WithTLS(tlsConfig *tls.Config, port int) func(s *Server) { return func(s *Server) { @@ -577,18 +585,14 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser // transaction ordering and prevents interleaved writes. go c.writeLoop() - // TODO: refactor this into a connection manager interface, maybe? - if s.Redis != nil { - s.Redis.SAdd(context.Background(), RedisKeyOnline, login+"::"+ipAddr) + if s.Presence != nil { + s.Presence.UserConnected(login, ipAddr) } // Remove the client from the list of connected clients when they disconnect defer func() { - if s.Redis != nil { - s.Redis.SRem(context.Background(), RedisKeyOnline, login+"::"+ipAddr) - if userName := c.GetUserName(); len(userName) != 0 { - s.Redis.SRem(context.Background(), RedisKeyOnline, login+":"+string(userName)+":"+ipAddr) - } + if s.Presence != nil { + s.Presence.UserDisconnected(login, string(c.GetUserName()), ipAddr) } c.Disconnect() }() @@ -665,12 +669,8 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser c.Logger = c.Logger.With("name", string(userName)) c.Logger.Info("Login successful") - // Update the Redis set with the new information - if s.Redis != nil { - // Remove old entry (login::ip) - s.Redis.SRem(context.Background(), RedisKeyOnline, login+"::"+ipAddr) - // Add new entry with login, nickname, ip - s.Redis.SAdd(context.Background(), RedisKeyOnline, login+":"+string(userName)+":"+ipAddr) + if s.Presence != nil { + s.Presence.UserRenamed(login, "", string(userName), ipAddr) } // Notify other clients on the server that the new user has logged in. For 1.5+ clients we don't have this |