From f6d82fac6f2f365e73254993782c490a6995fec3 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 10 Jul 2026 18:25:38 +0200 Subject: refactored handler funcs to have return values instead of writing to conn and to not take conn as a parameter --- main.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 80b78cb..570b512 100644 --- a/main.go +++ b/main.go @@ -15,16 +15,16 @@ const NumShards = 16 var db RedisDB var hub Hub -type handlerFunc func(net.Conn, []string) +type baseHandlerFunc func([]string)interface{} +type connectionHandlerFunc func(net.Conn, []string) -var commandRegistry = map[string]handlerFunc{ +var baseCommandRegistry = map[string]baseHandlerFunc{ "GET": handleGet, "SET": handleSet, "DEL": handleDel, "EXISTS": handleExists, "INCR": handleIncr, "DECR": handleDecr, - "PING": handlePing, "RENAME": handleRename, "FLUSHALL": handleFlushall, "RPUSH": handleRpush, @@ -42,9 +42,12 @@ var commandRegistry = map[string]handlerFunc{ "DBSIZE": handleDbsize, "KEYS": handleKeys, "LREM": handleLrem, +} +var connectionCommandRegistry = map[string]connectionHandlerFunc{ "SUBSCRIBE": handleSubscribe, "UNSUBSCRIBE": handleUnsubscribe, "PUBLISH": handlePublish, + "PING": handlePing, } func main() { @@ -115,8 +118,8 @@ func handleConnection(conn net.Conn) { command := strings.ToUpper(args[0]) - if handler, ok := commandRegistry[command]; ok { - handler(conn, args) + if handler, ok := baseCommandRegistry[command]; ok { + conn.Write(serializeRESP(handler(args))) } else { conn.Write(serializeRESP(errors.New("unknown command"))) } -- cgit v1.2.3