-
Notifications
You must be signed in to change notification settings - Fork 0
/
size_resource.go
57 lines (46 loc) · 1.29 KB
/
size_resource.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
package redis
// Handles creating a size limit.
//
// The underlying size limit is stored in REDIS. The size limit can be retrieved using 'Available()',
// which first attempts to get the limit via REDIS, then upstream if REDIS is unavailable.
import (
"context"
"fmt"
"github.com/datatrails/go-datatrails-common/logger"
)
type SizeResource struct {
*Resource
}
// Available gets current size limit.
func (sr *SizeResource) Available(ctx context.Context, tenantID string) (int64, error) {
return sr.getOperation(ctx, opGetLimit, tenantID)
}
// If limit is less than zero then the limit is disabled and all methods are noops.
func NewSizeResource(
cfg RedisConfig,
name string,
opts ...ResourceOption,
) (*SizeResource, error) {
logger.Sugar.Debugf("%s SizeResource", name)
client, err := NewRedisClient(cfg)
if err != nil {
return nil, fmt.Errorf("failed to create redis client for new size resource: %w", err)
}
r := &SizeResource{
Resource: &Resource{
ClientContext: ClientContext{
cfg: cfg,
name: name,
},
resourceLimiter: defaultResourceLimiter,
refreshTTL: defaultTTL,
refreshCount: defaultRefreshCount,
tenantLimits: make(map[string]*tenantLimit),
client: client,
},
}
for _, opt := range opts {
opt(r.Resource)
}
return r, nil
}