-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua-scripts.go
108 lines (92 loc) · 3.89 KB
/
lua-scripts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package redisSyncFanoutQueue
import (
"embed"
"fmt"
redisLuaScriptUtils "github.com/zavitax/redis-lua-script-utils-go"
)
var (
scriptCreateClientID *redisLuaScriptUtils.RedisScript
scriptUpdateClientTimestamp *redisLuaScriptUtils.RedisScript
scriptAddSyncClientToRoom *redisLuaScriptUtils.RedisScript
scriptRemoveSyncClientFromRoom *redisLuaScriptUtils.RedisScript
scriptSendOutOfBandRoomMessage *redisLuaScriptUtils.RedisScript
scriptConditionalProcessRoomMessages *redisLuaScriptUtils.RedisScript
scriptEnqueueRoomMessage *redisLuaScriptUtils.RedisScript
scriptAckClientMessage *redisLuaScriptUtils.RedisScript
scriptGetMetrics *redisLuaScriptUtils.RedisScript
scriptGetRoomState *redisLuaScriptUtils.RedisScript
)
//go:embed "internal/lua_scripts"
var scriptDir embed.FS
func init() {
luaFiles := []string{
"create_client_id.lua",
"update_client_timestamp.lua",
"add_sync_client_to_room.lua",
"remove_sync_client_from_room.lua",
"send_out_of_band_room_message.lua",
"conditional_process_room_messages.lua",
"enqueue_room_message.lua",
"ack_client_message.lua",
"get_metrics.lua",
"get_room_state.lua",
}
luaFileMap := make(map[string]string, len(luaFiles))
for _, filename := range luaFiles {
file, err := scriptDir.ReadFile(fmt.Sprintf("internal/lua_scripts/%s", filename))
if err != nil {
panic(err)
}
luaFileMap[filename] = string(file)
}
scriptCreateClientID = redisLuaScriptUtils.NewRedisScript(
[]string{"keyClientIDSequence", "keyLastTimestamp"},
[]string{"argCurrentTimestamp"},
luaFileMap["create_client_id.lua"],
)
scriptUpdateClientTimestamp = redisLuaScriptUtils.NewRedisScript(
[]string{"keyGlobalSetOfKnownClients", "keyRoomSetOfKnownClients"},
[]string{"argClientID", "argRoomID", "argCurrentTimestamp"},
luaFileMap["update_client_timestamp.lua"],
)
scriptAddSyncClientToRoom = redisLuaScriptUtils.NewRedisScript(
[]string{"keyGlobalSetOfKnownClients", "keyRoomSetOfKnownClients", "keyRoomSetOfAckedClients"},
[]string{"argClientID", "argRoomID", "argCurrentTimestamp"},
luaFileMap["add_sync_client_to_room.lua"],
)
scriptRemoveSyncClientFromRoom = redisLuaScriptUtils.NewRedisScript(
[]string{"keyGlobalSetOfKnownClients", "keyRoomSetOfKnownClients", "keyRoomSetOfAckedClients", "keyPubsubAdminEventsRemoveClientTopic"},
[]string{"argClientID", "argRoomID", "argCurrentTimestamp"},
luaFileMap["remove_sync_client_from_room.lua"],
)
scriptSendOutOfBandRoomMessage = redisLuaScriptUtils.NewRedisScript(
[]string{"keyRoomPubsub"},
[]string{"argRoomID", "argMsg"},
luaFileMap["send_out_of_band_room_message.lua"],
)
scriptConditionalProcessRoomMessages = redisLuaScriptUtils.NewRedisScript(
[]string{"keyRoomSetOfKnownClients", "keyRoomSetOfAckedClients", "keyGlobalKnownRooms", "keyRoomQueue", "keyRoomPubsub"},
[]string{"argRoomID"},
luaFileMap["conditional_process_room_messages.lua"],
)
scriptEnqueueRoomMessage = redisLuaScriptUtils.NewRedisScript(
[]string{"keyRoomSetOfKnownClients", "keyGlobalKnownRooms", "keyRoomQueue", "keyGlobalAckedRooms"},
[]string{"argRoomID", "argPriority", "argMsg"},
luaFileMap["enqueue_room_message.lua"],
)
scriptAckClientMessage = redisLuaScriptUtils.NewRedisScript(
[]string{"keyRoomSetOfKnownClients", "keyRoomSetOfAckedClients", "keyGlobalKnownRooms", "keyRoomQueue", "keyRoomPubsub"},
[]string{"argRoomID", "argClientID", "argCurrentTimestamp"},
luaFileMap["ack_client_message.lua"],
)
scriptGetMetrics = redisLuaScriptUtils.NewRedisScript(
[]string{"keyGlobalKnownRooms", "keyGlobalSetOfKnownClients"},
[]string{"argTopRoomsLimit"},
luaFileMap["get_metrics.lua"],
)
scriptGetRoomState = redisLuaScriptUtils.NewRedisScript(
[]string{"keyRoomQueue", "keyRoomSetOfKnownClients", "keyRoomSetOfAckedClients"},
[]string{"argRoomID"},
luaFileMap["get_room_state.lua"],
)
}