/
limit.go
81 lines (68 loc) · 1.5 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package datanode
import (
"context"
"fmt"
"golang.org/x/time/rate"
)
var (
deleteLimiteRater = rate.NewLimiter(rate.Inf, defaultMarkDeleteLimitBurst)
MaxExtentRepairLimit = 20000
MinExtentRepairLimit = 5
CurExtentRepairLimit = MaxExtentRepairLimit
extentRepairLimitRater chan struct{}
)
func initRepairLimit() {
extentRepairLimitRater = make(chan struct{}, MaxExtentRepairLimit)
for i := 0; i < MaxExtentRepairLimit; i++ {
extentRepairLimitRater <- struct{}{}
}
}
func requestDoExtentRepair() (err error) {
err = fmt.Errorf("repair limit, cannot do extentRepair")
select {
case <-extentRepairLimitRater:
return nil
default:
return
}
return
}
func fininshDoExtentRepair() {
select {
case extentRepairLimitRater <- struct{}{}:
return
default:
return
}
}
func setDoExtentRepair(value int) {
if value <= 0 {
value = MaxExtentRepairLimit
}
if value > MaxExtentRepairLimit {
value = MaxExtentRepairLimit
}
if value < MinExtentRepairLimit {
value = MinExtentRepairLimit
}
if CurExtentRepairLimit != value {
CurExtentRepairLimit = value
close(extentRepairLimitRater)
extentRepairLimitRater = make(chan struct{}, CurExtentRepairLimit)
for i := 0; i < CurExtentRepairLimit; i++ {
extentRepairLimitRater <- struct{}{}
}
}
}
func DeleteLimiterWait() {
ctx := context.Background()
deleteLimiteRater.Wait(ctx)
}
func setLimiter(limiter *rate.Limiter, limitValue uint64) {
r := limitValue
l := rate.Limit(r)
if r == 0 {
l = rate.Inf
}
limiter.SetLimit(l)
}