aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-10 18:25:38 +0200
committeralex <[email protected]>2026-07-10 18:25:38 +0200
commitf6d82fac6f2f365e73254993782c490a6995fec3 (patch)
tree77c37f77edd9ec7246961d4583842b3779aea91b /main.go
parent8f6257e55c2cb8f4c82fe51fbeaa4a8f66dc30ac (diff)
downloadredis-clone-f6d82fac6f2f365e73254993782c490a6995fec3.tar.xz
redis-clone-f6d82fac6f2f365e73254993782c490a6995fec3.zip
refactored handler funcs to have return values instead of writing to conn and to not take conn as a parameterpubsub
Diffstat (limited to 'main.go')
-rw-r--r--main.go13
1 files changed, 8 insertions, 5 deletions
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")))
}