-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
53 lines (46 loc) · 1.18 KB
/
token.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
package betterwatcher
import (
"encoding/json"
"github.com/go-redis/redis"
"go.mongodb.org/mongo-driver/bson"
)
func (factory WatcherFactory) storeToken(token *bson.Raw, redisKey string) error {
tokenJSON, err := json.Marshal(*token)
if err != nil {
return err
}
setResult := factory.redisClient.Set(redisKey, tokenJSON, 0)
return checkError(setResult.Err())
}
func (factory WatcherFactory) increaseRetries(token *bson.Raw) (int, error) {
tokenJSON, err := json.Marshal(*token)
if err != nil {
return 0, err
}
incrResult := factory.redisClient.Incr(string(tokenJSON) + "::retries")
if incrResult.Err() != nil {
return 0, incrResult.Err()
}
currentRetries, err := incrResult.Result()
if err != nil {
return 0, err
}
return int(currentRetries), nil
}
func (factory WatcherFactory) getToken(redisKey string) (bson.Raw, error) {
var resumeAfter bson.Raw
val, err := factory.redisClient.Get(redisKey).Result()
if err == nil {
err = json.Unmarshal([]byte(val), &resumeAfter)
if err != nil {
return nil, err
}
}
return resumeAfter, checkError(err)
}
func checkError(err error) error {
if err != nil && err != nil && err != redis.Nil {
return err
}
return nil
}