This repository has been archived by the owner on May 23, 2023. It is now read-only.
/
manager.go
242 lines (218 loc) · 6.05 KB
/
manager.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package quota
import (
"fmt"
"net/http"
"net/url"
"sync"
"time"
"github.com/apigee/istio-mixer-adapter/adapter/auth"
"github.com/apigee/istio-mixer-adapter/adapter/product"
"istio.io/istio/mixer/pkg/adapter"
)
const (
quotaPath = "/quotas"
defaultSyncRate = time.Second
defaultNumSyncWorkers = 10
defaultRefreshAfter = 1 * time.Minute
defaultDeleteAfter = 10 * time.Minute
syncQueueSize = 100
resultCacheBufferSize = 30
)
// A Manager tracks multiple Apigee quotas
type Manager struct {
baseURL *url.URL
close chan bool
closed chan bool
client *http.Client
now func() time.Time
log adapter.Logger
syncRate time.Duration
bucketsLock sync.RWMutex
buckets map[string]*bucket // Map from ID -> bucket
syncQueue chan *bucket
numSyncWorkers int
dupCache ResultCache
syncingBuckets map[*bucket]struct{}
syncingBucketsLock sync.Mutex
key string
secret string
}
// NewManager constructs and starts a new Manager. Call Close when done.
func NewManager(env adapter.Env, options Options) (*Manager, error) {
if err := options.validate(); err != nil {
return nil, err
}
m := newManager(options.BaseURL, options.Client, options.Key, options.Secret)
m.Start(env)
return m, nil
}
// newManager constructs a new Manager
func newManager(baseURL *url.URL, client *http.Client, key, secret string) *Manager {
return &Manager{
close: make(chan bool),
closed: make(chan bool),
client: client,
now: time.Now,
syncRate: defaultSyncRate,
buckets: map[string]*bucket{},
syncQueue: make(chan *bucket, syncQueueSize),
baseURL: baseURL,
numSyncWorkers: defaultNumSyncWorkers,
dupCache: ResultCache{size: resultCacheBufferSize},
syncingBuckets: map[*bucket]struct{}{},
key: key,
secret: secret,
}
}
// Start starts the manager.
func (m *Manager) Start(env adapter.Env) {
m.log = env.Logger()
m.log.Infof("starting quota manager")
env.ScheduleDaemon(func() {
m.syncLoop()
})
for i := 0; i < m.numSyncWorkers; i++ {
env.ScheduleDaemon(func() {
m.syncBucketWorker()
})
}
m.log.Infof("started quota manager with %d workers", m.numSyncWorkers)
}
// Close shuts down the manager.
func (m *Manager) Close() {
if m == nil {
return
}
m.log.Infof("closing quota manager")
m.close <- true
close(m.syncQueue)
for i := 0; i <= m.numSyncWorkers; i++ {
<-m.closed
}
m.log.Infof("closed quota manager")
}
func getQuotaID(auth *auth.Context, p *product.APIProduct) string {
return fmt.Sprintf("%s-%s", auth.Application, p.Name)
}
// Apply a quota request to the local quota bucket and schedule for sync
func (m *Manager) Apply(auth *auth.Context, p *product.APIProduct, args adapter.QuotaArgs) (*Result, error) {
if result := m.dupCache.Get(args.DeduplicationID); result != nil {
return result, nil
}
quotaID := getQuotaID(auth, p)
req := &Request{
Identifier: quotaID,
Interval: p.QuotaIntervalInt,
Allow: p.QuotaLimitInt,
TimeUnit: p.QuotaTimeUnit,
}
// a new bucket is created if missing or if product is no longer compatible
var result *Result
var err error
m.bucketsLock.RLock()
b, ok := m.buckets[quotaID]
m.bucketsLock.RUnlock()
if !ok || !b.compatible(req) {
m.bucketsLock.Lock()
b, ok = m.buckets[quotaID]
if !ok || !b.compatible(req) {
b = newBucket(*req, m)
m.buckets[quotaID] = b
m.log.Debugf("new quota bucket: %s", quotaID)
}
m.bucketsLock.Unlock()
}
req.Weight = args.QuotaAmount
result, err = b.apply(req)
if result != nil && err == nil && args.DeduplicationID != "" {
m.dupCache.Add(args.DeduplicationID, result)
}
return result, err
}
// loop to sync active buckets and deletes old buckets
func (m *Manager) syncLoop() {
t := time.NewTicker(m.syncRate)
for {
select {
case <-t.C:
var deleteIDs []string
m.bucketsLock.RLock()
for id, b := range m.buckets {
if b.needToDelete() {
deleteIDs = append(deleteIDs, id)
} else if b.needToSync() {
bucket := b
m.syncQueue <- bucket
}
}
m.bucketsLock.RUnlock()
if deleteIDs != nil {
m.log.Debugf("deleting quota buckets: %v", deleteIDs)
m.bucketsLock.Lock()
for _, id := range deleteIDs {
delete(m.buckets, id)
}
m.bucketsLock.Unlock()
}
case <-m.close:
m.log.Debugf("closing quota sync loop")
t.Stop()
m.closed <- true
return
}
}
}
// worker routine for syncing a bucket with the server
func (m *Manager) syncBucketWorker() {
for {
bucket, ok := <-m.syncQueue
if ok {
m.syncingBucketsLock.Lock()
if _, ok := m.syncingBuckets[bucket]; !ok {
m.syncingBuckets[bucket] = struct{}{}
m.syncingBucketsLock.Unlock()
bucket.sync()
m.syncingBucketsLock.Lock()
delete(m.syncingBuckets, bucket)
}
m.syncingBucketsLock.Unlock()
} else {
m.log.Debugf("closing quota sync worker")
m.closed <- true
return
}
}
}
// Options allows us to specify options for how this auth manager will run
type Options struct {
// Client is a configured HTTPClient
Client *http.Client
// BaseURL of the Apigee internal proxy
BaseURL *url.URL
// Key is provisioning key
Key string
// Secret is provisioning secret
Secret string
}
func (o *Options) validate() error {
if o.Client == nil ||
o.BaseURL == nil ||
o.Key == "" ||
o.Secret == "" {
return fmt.Errorf("all quota options are required")
}
return nil
}