From 8f6257e55c2cb8f4c82fe51fbeaa4a8f66dc30ac Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 9 Jul 2026 20:24:44 +0200 Subject: basic pubsub system --- main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'main.go') diff --git a/main.go b/main.go index 84974cd..80b78cb 100644 --- a/main.go +++ b/main.go @@ -13,6 +13,7 @@ import ( const NumShards = 16 var db RedisDB +var hub Hub type handlerFunc func(net.Conn, []string) @@ -41,6 +42,9 @@ var commandRegistry = map[string]handlerFunc{ "DBSIZE": handleDbsize, "KEYS": handleKeys, "LREM": handleLrem, + "SUBSCRIBE": handleSubscribe, + "UNSUBSCRIBE": handleUnsubscribe, + "PUBLISH": handlePublish, } func main() { @@ -53,6 +57,7 @@ func main() { } } db = RedisDB{shards: shards} + hub = Hub{Channels: make(map[string]map[net.Conn]struct{}),} go func() { ticker := time.NewTicker(1 * time.Second) @@ -88,7 +93,10 @@ func main() { } func handleConnection(conn net.Conn) { - defer conn.Close() + defer func() { + handleDisconnect(conn) + conn.Close() + }() fmt.Println("Client connected:", conn.RemoteAddr()) reader := bufio.NewReader(conn) -- cgit v1.2.3