Skip to content

Commit

Permalink
feat: make lua code simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
arusakov committed Oct 13, 2024
1 parent d63d0f2 commit bc29c71
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
14 changes: 4 additions & 10 deletions lua/channelAdd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ local chnl_key = ARGV[1]
local chnl_arg = ARGV[2]

local chnl_str = redis.call('HGET', key, chnl_key)
for match in chnl_str:gmatch('([^,]+)') do
if chnl_arg == match then
return 0
end
end
local index = string.find(chnl_str, ','..chnl_arg..',')

if chnl_str == '' then
chnl_str = chnl_arg
else
chnl_str = chnl_str .. ',' .. chnl_arg
if index then
return 0
end

redis.call('HSET', key, chnl_key, chnl_str)
redis.call('HSET', key, chnl_key, chnl_str..chnl_arg..',')

return 1
15 changes: 3 additions & 12 deletions lua/channelRemove.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,10 @@ local chnl_key = ARGV[1]
local chnl_arg = ARGV[2]

local chnl_str = redis.call('HGET', key, chnl_key)
local chnl, cnt = string.gsub(chnl_str, ','..chnl_arg..',', ',')

local chnl = {}
local removed = false
for match in chnl_str:gmatch('([^,]+)') do
if match ~= chnl_arg then
table.insert(chnl, match)
else
removed = true
end
end

if removed then
redis.call('HSET', key, chnl_key, table.concat(chnl, ','))
if cnt > 0 then
redis.call('HSET', key, chnl_key, chnl)
return 1
end

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export class WSDiscovery {
ttl || this.ttlClient,
SRVR, serverId,
SID, sessionId,
CHNL, '',
CHNL, ',',
)

return clientId
Expand All @@ -174,7 +174,7 @@ export class WSDiscovery {

async getClientChannels(clientId: number) {
const channels = await this.redis.hget(this.getClientKey(clientId), CHNL)
return channels ? channels.split(',') : []
return channels ? channels.split(',').slice(1, -1) : []
}

async updateClientTTL(clientId: number, ttl?: number) {
Expand Down

0 comments on commit bc29c71

Please sign in to comment.