-
Notifications
You must be signed in to change notification settings - Fork 182
/
api_limit.go
65 lines (52 loc) · 1.16 KB
/
api_limit.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
package api
// API限制
type APILimit struct {
Concurrent uint `yaml:"concurrent" json:"concurrent"` // 并发数
RequestLimits []*APIRequestLimit `yaml:"request" json:"request"` // 请求数限制 TODO
DataLimits []*APIDataLimit `yaml:"data" json:"data"` // 数据量限制 TODO
concurrentChan chan bool // 并发管道
concurrentIsLocked bool // 是否正在锁中
}
// 获取新的对象
func NewAPILimit() *APILimit {
return &APILimit{}
}
// 校验
func (this *APILimit) Validate() error {
// request
for _, reqLimit := range this.RequestLimits {
err := reqLimit.Validate()
if err != nil {
return err
}
}
// data
for _, dataLimit := range this.DataLimits {
err := dataLimit.Validate()
if err != nil {
return err
}
}
// 管道
if this.Concurrent > 0 {
this.concurrentChan = make(chan bool, this.Concurrent)
}
return nil
}
// 等待并发
func (this *APILimit) Begin() {
// concurrent
if this.Concurrent > 0 {
this.concurrentChan <- true
}
// request
// data
}
func (this *APILimit) Done() {
// concurrent
if this.Concurrent > 0 {
<-this.concurrentChan
}
// request
// data
}