Skip to content

Commit ac97ea5

Browse files
6543zeripath
andauthored
[Vendor] Update go-redis to v8.5.0 (#13749)
* Update go-redis to v8.4.0 * github.com/go-redis/redis/v8 v8.4.0 -> v8.5.0 * Apply suggestions from code review Co-authored-by: zeripath <[email protected]> * TODO * Use the Queue termination channel as the default context for pushes Signed-off-by: Andrew Thornton <[email protected]> * missed one Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: zeripath <[email protected]>
1 parent 4cffc46 commit ac97ea5

File tree

139 files changed

+16110
-4958
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+16110
-4958
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ require (
3535
github.com/go-git/go-billy/v5 v5.0.0
3636
github.com/go-git/go-git/v5 v5.2.0
3737
github.com/go-ldap/ldap/v3 v3.2.4
38-
github.com/go-redis/redis/v7 v7.4.0
38+
github.com/go-redis/redis/v8 v8.5.0
3939
github.com/go-sql-driver/mysql v1.5.0
4040
github.com/go-swagger/go-swagger v0.26.0
4141
github.com/go-testfixtures/testfixtures/v3 v3.4.1

go.sum

Lines changed: 13 additions & 33 deletions
Large diffs are not rendered by default.

modules/cache/cache_redis.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
"fmt"
99
"time"
1010

11+
"code.gitea.io/gitea/modules/graceful"
1112
"code.gitea.io/gitea/modules/nosql"
1213

1314
"gitea.com/go-chi/cache"
14-
"github.com/go-redis/redis/v7"
15+
"github.com/go-redis/redis/v8"
1516
"github.com/unknwon/com"
1617
)
1718

@@ -28,28 +29,28 @@ type RedisCacher struct {
2829
func (c *RedisCacher) Put(key string, val interface{}, expire int64) error {
2930
key = c.prefix + key
3031
if expire == 0 {
31-
if err := c.c.Set(key, com.ToStr(val), 0).Err(); err != nil {
32+
if err := c.c.Set(graceful.GetManager().HammerContext(), key, com.ToStr(val), 0).Err(); err != nil {
3233
return err
3334
}
3435
} else {
3536
dur, err := time.ParseDuration(com.ToStr(expire) + "s")
3637
if err != nil {
3738
return err
3839
}
39-
if err = c.c.Set(key, com.ToStr(val), dur).Err(); err != nil {
40+
if err = c.c.Set(graceful.GetManager().HammerContext(), key, com.ToStr(val), dur).Err(); err != nil {
4041
return err
4142
}
4243
}
4344

4445
if c.occupyMode {
4546
return nil
4647
}
47-
return c.c.HSet(c.hsetName, key, "0").Err()
48+
return c.c.HSet(graceful.GetManager().HammerContext(), c.hsetName, key, "0").Err()
4849
}
4950

5051
// Get gets cached value by given key.
5152
func (c *RedisCacher) Get(key string) interface{} {
52-
val, err := c.c.Get(c.prefix + key).Result()
53+
val, err := c.c.Get(graceful.GetManager().HammerContext(), c.prefix+key).Result()
5354
if err != nil {
5455
return nil
5556
}
@@ -59,58 +60,58 @@ func (c *RedisCacher) Get(key string) interface{} {
5960
// Delete deletes cached value by given key.
6061
func (c *RedisCacher) Delete(key string) error {
6162
key = c.prefix + key
62-
if err := c.c.Del(key).Err(); err != nil {
63+
if err := c.c.Del(graceful.GetManager().HammerContext(), key).Err(); err != nil {
6364
return err
6465
}
6566

6667
if c.occupyMode {
6768
return nil
6869
}
69-
return c.c.HDel(c.hsetName, key).Err()
70+
return c.c.HDel(graceful.GetManager().HammerContext(), c.hsetName, key).Err()
7071
}
7172

7273
// Incr increases cached int-type value by given key as a counter.
7374
func (c *RedisCacher) Incr(key string) error {
7475
if !c.IsExist(key) {
7576
return fmt.Errorf("key '%s' not exist", key)
7677
}
77-
return c.c.Incr(c.prefix + key).Err()
78+
return c.c.Incr(graceful.GetManager().HammerContext(), c.prefix+key).Err()
7879
}
7980

8081
// Decr decreases cached int-type value by given key as a counter.
8182
func (c *RedisCacher) Decr(key string) error {
8283
if !c.IsExist(key) {
8384
return fmt.Errorf("key '%s' not exist", key)
8485
}
85-
return c.c.Decr(c.prefix + key).Err()
86+
return c.c.Decr(graceful.GetManager().HammerContext(), c.prefix+key).Err()
8687
}
8788

8889
// IsExist returns true if cached value exists.
8990
func (c *RedisCacher) IsExist(key string) bool {
90-
if c.c.Exists(c.prefix+key).Val() == 1 {
91+
if c.c.Exists(graceful.GetManager().HammerContext(), c.prefix+key).Val() == 1 {
9192
return true
9293
}
9394

9495
if !c.occupyMode {
95-
c.c.HDel(c.hsetName, c.prefix+key)
96+
c.c.HDel(graceful.GetManager().HammerContext(), c.hsetName, c.prefix+key)
9697
}
9798
return false
9899
}
99100

100101
// Flush deletes all cached data.
101102
func (c *RedisCacher) Flush() error {
102103
if c.occupyMode {
103-
return c.c.FlushDB().Err()
104+
return c.c.FlushDB(graceful.GetManager().HammerContext()).Err()
104105
}
105106

106-
keys, err := c.c.HKeys(c.hsetName).Result()
107+
keys, err := c.c.HKeys(graceful.GetManager().HammerContext(), c.hsetName).Result()
107108
if err != nil {
108109
return err
109110
}
110-
if err = c.c.Del(keys...).Err(); err != nil {
111+
if err = c.c.Del(graceful.GetManager().HammerContext(), keys...).Err(); err != nil {
111112
return err
112113
}
113-
return c.c.Del(c.hsetName).Err()
114+
return c.c.Del(graceful.GetManager().HammerContext(), c.hsetName).Err()
114115
}
115116

116117
// StartAndGC starts GC routine based on config string settings.
@@ -132,7 +133,7 @@ func (c *RedisCacher) StartAndGC(opts cache.Options) error {
132133
}
133134
}
134135

135-
return c.c.Ping().Err()
136+
return c.c.Ping(graceful.GetManager().HammerContext()).Err()
136137
}
137138

138139
func init() {

modules/nosql/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12-
"github.com/go-redis/redis/v7"
12+
"github.com/go-redis/redis/v8"
1313
"github.com/syndtr/goleveldb/leveldb"
1414
)
1515

modules/nosql/manager_redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"strconv"
1111
"strings"
1212

13-
"github.com/go-redis/redis/v7"
13+
"github.com/go-redis/redis/v8"
1414
)
1515

1616
var replacer = strings.NewReplacer("_", "", "-", "")

modules/queue/queue_bytefifo.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ func (q *ByteFIFOQueue) Shutdown() {
163163
log.Debug("%s: %s Shutdown", q.typ, q.name)
164164
}
165165

166+
// IsShutdown returns a channel which is closed when this Queue is shutdown
167+
func (q *ByteFIFOQueue) IsShutdown() <-chan struct{} {
168+
return q.closed
169+
}
170+
166171
// Terminate this queue and close the queue
167172
func (q *ByteFIFOQueue) Terminate() {
168173
log.Trace("%s: %s Terminating", q.typ, q.name)
@@ -185,6 +190,11 @@ func (q *ByteFIFOQueue) Terminate() {
185190
log.Debug("%s: %s Terminated", q.typ, q.name)
186191
}
187192

193+
// IsTerminated returns a channel which is closed when this Queue is terminated
194+
func (q *ByteFIFOQueue) IsTerminated() <-chan struct{} {
195+
return q.terminated
196+
}
197+
188198
var _ (UniqueQueue) = &ByteFIFOUniqueQueue{}
189199

190200
// ByteFIFOUniqueQueue represents a UniqueQueue formed from a UniqueByteFifo

modules/queue/queue_redis.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
package queue
66

77
import (
8+
"context"
9+
"fmt"
10+
11+
"code.gitea.io/gitea/modules/graceful"
812
"code.gitea.io/gitea/modules/log"
913
"code.gitea.io/gitea/modules/nosql"
1014

11-
"github.com/go-redis/redis/v7"
15+
"github.com/go-redis/redis/v8"
1216
)
1317

1418
// RedisQueueType is the type for redis queue
@@ -43,6 +47,8 @@ func NewRedisQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error)
4347
return nil, err
4448
}
4549

50+
byteFIFO.ctx = graceful.NewChannelContext(byteFIFOQueue.IsTerminated(), fmt.Errorf("queue has been terminated"))
51+
4652
queue := &RedisQueue{
4753
ByteFIFOQueue: byteFIFOQueue,
4854
}
@@ -53,20 +59,21 @@ func NewRedisQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue, error)
5359
}
5460

5561
type redisClient interface {
56-
RPush(key string, args ...interface{}) *redis.IntCmd
57-
LPop(key string) *redis.StringCmd
58-
LLen(key string) *redis.IntCmd
59-
SAdd(key string, members ...interface{}) *redis.IntCmd
60-
SRem(key string, members ...interface{}) *redis.IntCmd
61-
SIsMember(key string, member interface{}) *redis.BoolCmd
62-
Ping() *redis.StatusCmd
62+
RPush(ctx context.Context, key string, args ...interface{}) *redis.IntCmd
63+
LPop(ctx context.Context, key string) *redis.StringCmd
64+
LLen(ctx context.Context, key string) *redis.IntCmd
65+
SAdd(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
66+
SRem(ctx context.Context, key string, members ...interface{}) *redis.IntCmd
67+
SIsMember(ctx context.Context, key string, member interface{}) *redis.BoolCmd
68+
Ping(ctx context.Context) *redis.StatusCmd
6369
Close() error
6470
}
6571

6672
var _ (ByteFIFO) = &RedisByteFIFO{}
6773

6874
// RedisByteFIFO represents a ByteFIFO formed from a redisClient
6975
type RedisByteFIFO struct {
76+
ctx context.Context
7077
client redisClient
7178
queueName string
7279
}
@@ -82,8 +89,9 @@ func NewRedisByteFIFO(config RedisByteFIFOConfiguration) (*RedisByteFIFO, error)
8289
fifo := &RedisByteFIFO{
8390
queueName: config.QueueName,
8491
}
92+
fifo.ctx = graceful.GetManager().TerminateContext()
8593
fifo.client = nosql.GetManager().GetRedisClient(config.ConnectionString)
86-
if err := fifo.client.Ping().Err(); err != nil {
94+
if err := fifo.client.Ping(graceful.GetManager().ShutdownContext()).Err(); err != nil {
8795
return nil, err
8896
}
8997
return fifo, nil
@@ -96,12 +104,12 @@ func (fifo *RedisByteFIFO) PushFunc(data []byte, fn func() error) error {
96104
return err
97105
}
98106
}
99-
return fifo.client.RPush(fifo.queueName, data).Err()
107+
return fifo.client.RPush(fifo.ctx, fifo.queueName, data).Err()
100108
}
101109

102110
// Pop pops data from the start of the fifo
103111
func (fifo *RedisByteFIFO) Pop() ([]byte, error) {
104-
data, err := fifo.client.LPop(fifo.queueName).Bytes()
112+
data, err := fifo.client.LPop(fifo.ctx, fifo.queueName).Bytes()
105113
if err == nil || err == redis.Nil {
106114
return data, nil
107115
}
@@ -115,7 +123,7 @@ func (fifo *RedisByteFIFO) Close() error {
115123

116124
// Len returns the length of the fifo
117125
func (fifo *RedisByteFIFO) Len() int64 {
118-
val, err := fifo.client.LLen(fifo.queueName).Result()
126+
val, err := fifo.client.LLen(fifo.ctx, fifo.queueName).Result()
119127
if err != nil {
120128
log.Error("Error whilst getting length of redis queue %s: Error: %v", fifo.queueName, err)
121129
return -1

modules/queue/unique_queue_redis.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44

55
package queue
66

7-
import "github.com/go-redis/redis/v7"
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/modules/graceful"
11+
"github.com/go-redis/redis/v8"
12+
)
813

914
// RedisUniqueQueueType is the type for redis queue
1015
const RedisUniqueQueueType Type = "unique-redis"
@@ -46,6 +51,8 @@ func NewRedisUniqueQueue(handle HandlerFunc, cfg, exemplar interface{}) (Queue,
4651
return nil, err
4752
}
4853

54+
byteFIFO.ctx = graceful.NewChannelContext(byteFIFOQueue.IsTerminated(), fmt.Errorf("queue has been terminated"))
55+
4956
queue := &RedisUniqueQueue{
5057
ByteFIFOUniqueQueue: byteFIFOQueue,
5158
}
@@ -86,7 +93,7 @@ func NewRedisUniqueByteFIFO(config RedisUniqueByteFIFOConfiguration) (*RedisUniq
8693

8794
// PushFunc pushes data to the end of the fifo and calls the callback if it is added
8895
func (fifo *RedisUniqueByteFIFO) PushFunc(data []byte, fn func() error) error {
89-
added, err := fifo.client.SAdd(fifo.setName, data).Result()
96+
added, err := fifo.client.SAdd(fifo.ctx, fifo.setName, data).Result()
9097
if err != nil {
9198
return err
9299
}
@@ -98,12 +105,12 @@ func (fifo *RedisUniqueByteFIFO) PushFunc(data []byte, fn func() error) error {
98105
return err
99106
}
100107
}
101-
return fifo.client.RPush(fifo.queueName, data).Err()
108+
return fifo.client.RPush(fifo.ctx, fifo.queueName, data).Err()
102109
}
103110

104111
// Pop pops data from the start of the fifo
105112
func (fifo *RedisUniqueByteFIFO) Pop() ([]byte, error) {
106-
data, err := fifo.client.LPop(fifo.queueName).Bytes()
113+
data, err := fifo.client.LPop(fifo.ctx, fifo.queueName).Bytes()
107114
if err != nil && err != redis.Nil {
108115
return data, err
109116
}
@@ -112,13 +119,13 @@ func (fifo *RedisUniqueByteFIFO) Pop() ([]byte, error) {
112119
return data, nil
113120
}
114121

115-
err = fifo.client.SRem(fifo.setName, data).Err()
122+
err = fifo.client.SRem(fifo.ctx, fifo.setName, data).Err()
116123
return data, err
117124
}
118125

119126
// Has returns whether the fifo contains this data
120127
func (fifo *RedisUniqueByteFIFO) Has(data []byte) (bool, error) {
121-
return fifo.client.SIsMember(fifo.setName, data).Result()
128+
return fifo.client.SIsMember(fifo.ctx, fifo.setName, data).Result()
122129
}
123130

124131
func init() {

0 commit comments

Comments
 (0)