-
Notifications
You must be signed in to change notification settings - Fork 0
/
andromeda.go
165 lines (138 loc) · 4.57 KB
/
andromeda.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
163
164
165
package andromeda
import (
"context"
"time"
)
// QuotaRequest is a model for quota request
type QuotaRequest struct {
QuotaID string
Data interface{}
}
// QuotaUsageRequest is a model for quota usage request
type QuotaUsageRequest struct {
QuotaID string
Usage int64
Data interface{}
}
// UpdateQuotaUsage is a contract to update quota usage
// example: add quota usage or reduce quota usage
type UpdateQuotaUsage interface {
Do(ctx context.Context, req *QuotaUsageRequest) (interface{}, error)
}
// UpdateQuotaUsageListener listen on success or error when updating quota usage
type UpdateQuotaUsageListener interface {
OnSuccess(ctx context.Context, req *QuotaUsageRequest, updatedUsage int64)
OnError(ctx context.Context, req *QuotaUsageRequest, err error)
}
// GetQuota is a contract to get quota limit or usage
type GetQuota interface {
Do(ctx context.Context, req *QuotaRequest) (int64, error)
}
// GetQuotaKey is a contract to get quota key for the cache
type GetQuotaKey interface {
Do(ctx context.Context, req *QuotaRequest) (string, error)
}
// GetQuotaExpiration is a contract to get quota expiration for the cache
type GetQuotaExpiration interface {
Do(ctx context.Context, req *QuotaRequest) (time.Duration, error)
}
// XSetNXQuota is a contract to check exists or set if not exists for quota
type XSetNXQuota interface {
Do(ctx context.Context, req *QuotaRequest) error
}
// AddQuotaUsageConfig .
type AddQuotaUsageConfig struct {
Next UpdateQuotaUsage
Cache Cache
GetQuotaLimit GetQuota
GetQuotaUsage GetQuota
GetQuotaUsageKey GetQuotaKey
GetQuotaUsageExpiration GetQuotaExpiration
GetQuotaUsageConfig GetQuotaUsageConfig
Option AddUsageOption
}
// ReduceQuotaUsageConfig .
type ReduceQuotaUsageConfig struct {
Next UpdateQuotaUsage
Cache Cache
GetQuotaUsage GetQuota
GetQuotaUsageKey GetQuotaKey
GetQuotaUsageExpiration GetQuotaExpiration
GetQuotaUsageConfig GetQuotaUsageConfig
Option ReduceUsageOption
}
// GetQuotaUsageConfig .
type GetQuotaUsageConfig struct {
LockIn time.Duration
MaxRetry int
RetryIn time.Duration
}
// GetLockIn .
func (q GetQuotaUsageConfig) GetLockIn() time.Duration {
if q.LockIn.Milliseconds() > 0 {
return q.LockIn
}
return time.Second * 1
}
func (q GetQuotaUsageConfig) GetMaxRetry() int {
if q.MaxRetry > 0 {
return q.MaxRetry
}
return 1
}
// GetRetryIn .
func (q GetQuotaUsageConfig) GetRetryIn() time.Duration {
if q.RetryIn.Milliseconds() > 0 {
return q.RetryIn
}
return time.Millisecond * 50
}
// AddQuotaUsage .
func AddQuotaUsage(conf AddQuotaUsageConfig) UpdateQuotaUsage {
if conf.Cache == nil {
panic("Cache is required")
}
if conf.GetQuotaLimit == nil {
panic("GetQuotaLimit is required")
}
if conf.GetQuotaUsageKey == nil {
panic("GetQuotaUsageKey is required")
}
if conf.Next == nil {
conf.Next = NopUpdateQuotaUsage()
}
addQuotaUsage := NewAddQuotaUsage(conf.Cache, conf.GetQuotaUsageKey, conf.GetQuotaLimit, conf.Next, conf.Option)
if conf.GetQuotaUsage != nil {
if conf.GetQuotaUsageExpiration == nil {
panic("GetQuotaUsageExpiration is required")
}
getUsageConf := conf.GetQuotaUsageConfig
xSetNXQuotaUsage := NewXSetNXQuota(conf.Cache, conf.GetQuotaUsageKey, conf.GetQuotaUsageExpiration, conf.GetQuotaUsage, getUsageConf.GetLockIn())
xSetNXQuotaUsage = NewRetryableXSetNXQuota(xSetNXQuotaUsage, getUsageConf.GetMaxRetry(), getUsageConf.GetRetryIn())
addQuotaUsage = NewUpdateQuotaUsageMiddleware(NewXSetNXQuotaUsage(xSetNXQuotaUsage), addQuotaUsage)
}
return addQuotaUsage
}
// ReduceQuotaUsage .
func ReduceQuotaUsage(conf ReduceQuotaUsageConfig) UpdateQuotaUsage {
if conf.Cache == nil {
panic("Cache is required")
}
if conf.GetQuotaUsageKey == nil {
panic("GetQuotaUsageKey is required")
}
if conf.Next == nil {
conf.Next = NopUpdateQuotaUsage()
}
reduceQuotaUsage := NewReduceQuotaUsage(conf.Cache, conf.GetQuotaUsageKey, conf.Next, conf.Option)
if conf.GetQuotaUsage != nil {
if conf.GetQuotaUsageExpiration == nil {
panic("GetQuotaUsageExpiration is required")
}
getUsageConf := conf.GetQuotaUsageConfig
xSetNXQuotaUsage := NewXSetNXQuota(conf.Cache, conf.GetQuotaUsageKey, conf.GetQuotaUsageExpiration, conf.GetQuotaUsage, getUsageConf.GetLockIn())
xSetNXQuotaUsage = NewRetryableXSetNXQuota(xSetNXQuotaUsage, getUsageConf.GetMaxRetry(), getUsageConf.GetRetryIn())
reduceQuotaUsage = NewUpdateQuotaUsageMiddleware(NewXSetNXQuotaUsage(xSetNXQuotaUsage), reduceQuotaUsage)
}
return reduceQuotaUsage
}