-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
156 lines (123 loc) · 3.89 KB
/
utils.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package rds
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/goharbor/harbor/src/jobservice/common/utils"
"github.com/pkg/errors"
"time"
)
// ErrNoElements is a pre defined error to describe the case that no elements got
// from the backend database.
var ErrNoElements = errors.New("no elements got from the backend")
// HmSet sets the properties of hash map
func HmSet(conn redis.Conn, key string, fieldAndValues ...interface{}) error {
if conn == nil {
return errors.New("nil redis connection")
}
if utils.IsEmptyStr(key) {
return errors.New("no key specified to do HMSET")
}
if len(fieldAndValues) == 0 {
return errors.New("no properties specified to do HMSET")
}
args := make([]interface{}, 0, len(fieldAndValues)+2)
args = append(args, key)
args = append(args, fieldAndValues...)
args = append(args, "update_time", time.Now().Unix()) // Add update timestamp
_, err := conn.Do("HMSET", args...)
return err
}
// HmGet gets values of multiple fields
// Values have same order with the provided fields
func HmGet(conn redis.Conn, key string, fields ...interface{}) ([]interface{}, error) {
if conn == nil {
return nil, errors.New("nil redis connection")
}
if utils.IsEmptyStr(key) {
return nil, errors.New("no key specified to do HMGET")
}
if len(fields) == 0 {
return nil, errors.New("no fields specified to do HMGET")
}
args := make([]interface{}, 0, len(fields)+1)
args = append(args, key)
args = append(args, fields...)
return redis.Values(conn.Do("HMGET", args...))
}
// JobScore represents the data item with score in the redis db.
type JobScore struct {
JobBytes []byte
Score int64
}
// GetZsetByScore get the items from the zset filtered by the specified score scope.
func GetZsetByScore(conn redis.Conn, key string, scores []int64) ([]JobScore, error) {
if conn == nil {
return nil, errors.New("nil redis conn when getting zset by score")
}
if utils.IsEmptyStr(key) {
return nil, errors.New("missing key when getting zset by score")
}
if len(scores) < 2 {
return nil, errors.New("bad arguments: not enough scope scores provided")
}
values, err := redis.Values(conn.Do("ZRANGEBYSCORE", key, scores[0], scores[1], "WITHSCORES"))
if err != nil {
return nil, err
}
var jobsWithScores []JobScore
if err := redis.ScanSlice(values, &jobsWithScores); err != nil {
return nil, err
}
return jobsWithScores, nil
}
// AcquireLock acquires a redis lock with specified expired time
func AcquireLock(conn redis.Conn, lockerKey string, lockerID string, expireTime int64) error {
args := []interface{}{lockerKey, lockerID, "NX", "EX", expireTime}
res, err := conn.Do("SET", args...)
if err != nil {
return err
}
// Existing, the value can not be override
if res == nil {
return fmt.Errorf("key %s is already set with value %v", lockerKey, lockerID)
}
return nil
}
// ReleaseLock releases the acquired lock
func ReleaseLock(conn redis.Conn, lockerKey string, lockerID string) error {
theID, err := redis.String(conn.Do("GET", lockerKey))
if err != nil {
return err
}
if theID == lockerID {
_, err := conn.Do("DEL", lockerKey)
return err
}
return errors.New("locker ID mismatch")
}
// ZPopMin pops the element with lowest score in the zset
func ZPopMin(conn redis.Conn, key string) (interface{}, error) {
err := conn.Send("MULTI")
err = conn.Send("ZRANGE", key, 0, 0) // lowest one
err = conn.Send("ZREMRANGEBYRANK", key, 0, 0)
if err != nil {
return nil, err
}
replies, err := redis.Values(conn.Do("EXEC"))
if err != nil {
return nil, err
}
if len(replies) < 2 {
return nil, errors.Errorf("zpopmin error: not enough results returned, expected %d but got %d", 2, len(replies))
}
zrangeReply := replies[0]
if zrangeReply != nil {
if elements, ok := zrangeReply.([]interface{}); ok {
if len(elements) == 0 {
return nil, ErrNoElements
}
return elements[0], nil
}
}
return nil, errors.New("zpopmin error: bad result reply")
}