forked from saiya/dsps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pubsub_subscriber_lua.go
58 lines (50 loc) · 1.89 KB
/
pubsub_subscriber_lua.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
package redis
import (
"context"
"errors"
"github.com/go-redis/redis/v8"
"golang.org/x/xerrors"
"github.com/m3dev/dsps/server/domain"
"github.com/m3dev/dsps/server/logger"
internal "github.com/m3dev/dsps/server/storage/redis/internal"
)
func (s *redisStorage) loadPubSubSubscriberScripts(ctx context.Context) error {
if err := s.RedisCmd.LoadScript(ctx, createSubscriberScript); err != nil {
return xerrors.Errorf("Failed to load createSubscriberScript: %w", err)
}
return nil
}
// @returns "OK" (Redis status reply) if succeeded
// @returns false (Nil bulk reply) if already exists
var createSubscriberScript = redis.NewScript(`
local clockKey = KEYS[1] -- Clock (c.{{channel}}.clock)
local subscriberKey = KEYS[2] -- XXXX (c.{{channel}}.r.{subscriber})
local ttlSec = tonumber(ARGV[1]) -- (number) ttl [sec]
local chClock = tonumber(redis.call("get", clockKey))
if chClock == nil then
chClock = 0
redis.call("set", clockKey, 0, "EX", ttlSec) -- Create channel
else
redis.call("expire", clockKey, ttlSec) -- Extend channel life
end
-- Create subscriber if not exists
return redis.call("set", subscriberKey, string.format("%d", chClock), "EX", ttlSec, "NX")
`)
func runCreateSubscriberScript(ctx context.Context, redisCmd internal.RedisCmd, channelID domain.ChannelID, ttl channelTTLSec, sbscID domain.SubscriberID) error {
keys := keyOfChannel(channelID)
result, err := redisCmd.RunScript(
ctx, createSubscriberScript,
[]string{keys.Clock(), keys.SubscriberCursor(sbscID)},
ttl,
)
if err != nil {
if errors.Is(err, redis.Nil) {
logger.Of(ctx).Debugf(logger.CatStorage, "Subscriber already exists: %s on %s", sbscID, keys.channelID)
} else {
return xerrors.Errorf("Failed to execute createSubscriberScript: %w", err)
}
} else if result != "OK" {
return xerrors.Errorf("Unexpected result from createSubscriberScript: %T(%v)", result, result)
}
return nil
}