-
Notifications
You must be signed in to change notification settings - Fork 61
/
redis.go
65 lines (51 loc) · 1.74 KB
/
redis.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
// SPDX-License-Identifier: AGPL-3.0-only
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package driver
import (
"context"
"time"
"github.com/redis/go-redis/v9"
"github.com/trim21/errgo"
"go.uber.org/zap"
"github.com/bangumi/server/config"
"github.com/bangumi/server/internal/metrics"
"github.com/bangumi/server/internal/pkg/logger"
)
const defaultRedisPoolSize = 4
// NewRedisClient create a redis client
// use [test.GetRedis] in tests.
func NewRedisClient(c config.AppConfig) (*redis.Client, error) {
redisOptions, err := redis.ParseURL(c.RedisURL)
if err != nil {
logger.Fatal("redis: failed to parse redis url", zap.String("url", c.RedisURL))
}
if redisOptions.PoolSize == 0 {
redisOptions.PoolSize = defaultRedisPoolSize
}
cli := redis.NewClient(redisOptions)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := cli.Ping(ctx).Err(); err != nil {
return nil, errgo.Wrap(err, "redis: failed to ping")
}
return cli, nil
}
func NewRedisClientWithMetrics(c config.AppConfig) (*redis.Client, error) {
cli, err := NewRedisClient(c)
if err != nil {
return cli, err
}
cli.AddHook(metrics.RedisHook(cli.Options().Addr))
return cli, nil
}