This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
162 lines (139 loc) · 4.15 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
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
157
158
159
160
161
162
// 🔬 chi-ratelimit-redis: Redis support for the chi-ratelimit library.
// Copyright (c) 2022 Noelware
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Package redis is the main package to use a Redis server to persist
// ratelimits.
package redis
import (
"context"
"encoding/json"
"errors"
"github.com/go-redis/redis/v8"
"github.com/noelware/chi-ratelimit/providers"
"github.com/noelware/chi-ratelimit/types"
"time"
)
// Provider is the main providers.Provider object to implement when using
// this library.
type Provider struct {
keyPrefix string
client *redis.Client
}
type options struct {
keyPrefix string
client *redis.Client
}
// WithKeyPrefix appends a new key prefix to use when constructing
// a Provider.
func WithKeyPrefix(prefix string) func(o *options) {
return func(o *options) {
o.keyPrefix = prefix
}
}
// WithClient appends a pre-existing Redis client that is connected
// when constructing a Provider.
func WithClient(client *redis.Client) func(o *options) {
return func(o *options) {
o.client = client
}
}
// WithConfig creates and connects a new Redis client and appends it
// to the Provider.
func WithConfig(config *redis.Options) (func(o *options), error) {
ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
defer cancel()
client := redis.NewClient(config)
if err := client.Ping(ctx).Err(); err != nil {
// TODO: find a better solution for this
// no-op operation
return func(o *options) {}, nil
}
return func(o *options) {
o.client = client
}, nil
}
// New creates a new Provider object with the following options that was
// passed down.
func New(opts ...func(o *options)) (providers.Provider, error) {
config := &options{
keyPrefix: "chi_ratelimit",
client: nil,
}
for _, override := range opts {
override(config)
}
if config.client == nil {
return nil, errors.New("missing redis client to use")
}
return &Provider{
keyPrefix: config.keyPrefix,
client: config.client,
}, nil
}
func (p *Provider) Reset(key string) (bool, error) {
// Check if it exists
ok, err := p.client.HExists(context.TODO(), p.keyPrefix, key).Result()
if err != nil {
return false, err
}
if !ok {
return false, nil
}
// Delete it from Redis
if err := p.client.HDel(context.TODO(), p.keyPrefix, key).Err(); err != nil {
return false, err
} else {
return true, nil
}
}
func (*Provider) Name() string {
return "redis provider"
}
func (p *Provider) Put(key string, value *types.Ratelimit) error {
data, err := json.Marshal(value)
if err != nil {
return err
}
if err := p.client.HMSet(context.TODO(), p.keyPrefix, key, string(data)).Err(); err != nil {
return err
} else {
return nil
}
}
func (p *Provider) Get(key string) (*types.Ratelimit, error) {
// Update the database with the new copy
data, err := p.client.HGet(context.TODO(), p.keyPrefix, key).Result()
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, nil
} else {
return nil, err
}
}
var rl *types.Ratelimit
if err := json.Unmarshal([]byte(data), &rl); err != nil {
return nil, err
}
copied := rl.Copy()
if err := p.Put(key, copied); err != nil {
return nil, err
}
return copied, nil
}