-
Notifications
You must be signed in to change notification settings - Fork 35
/
gater_options.go
101 lines (95 loc) · 3.35 KB
/
gater_options.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
package gater
import (
"strings"
"github.com/bnb-chain/greenfield-storage-provider/base/gfspapp"
"github.com/bnb-chain/greenfield-storage-provider/base/gfspconfig"
coremodule "github.com/bnb-chain/greenfield-storage-provider/core/module"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
mwhttp "github.com/bnb-chain/greenfield-storage-provider/pkg/middleware/http"
)
const (
DefaultGatewayDomainName = "localhost:9133"
DefaultMaxListReadQuota = 100
DefaultMaxPayloadSize = 2 * 1024 * 1024 * 1024
)
func NewGateModular(app *gfspapp.GfSpBaseApp, cfg *gfspconfig.GfSpConfig) (coremodule.Modular, error) {
gater := &GateModular{baseApp: app}
if err := defaultGaterOptions(gater, cfg); err != nil {
return nil, err
}
return gater, nil
}
func defaultGaterOptions(gater *GateModular, cfg *gfspconfig.GfSpConfig) error {
if cfg.Gateway.DomainName == "" {
cfg.Gateway.DomainName = DefaultGatewayDomainName
}
if cfg.Gateway.HTTPAddress == "" {
cfg.Gateway.HTTPAddress = DefaultGatewayDomainName
}
if cfg.Bucket.MaxListReadQuotaNumber == 0 {
cfg.Bucket.MaxListReadQuotaNumber = DefaultMaxListReadQuota
}
if cfg.Bucket.MaxPayloadSize == 0 {
cfg.Bucket.MaxPayloadSize = DefaultMaxPayloadSize
}
gater.maxPayloadSize = cfg.Bucket.MaxPayloadSize
gater.env = cfg.Env
gater.domain = cfg.Gateway.DomainName
gater.httpAddress = cfg.Gateway.HTTPAddress
gater.maxListReadQuota = cfg.Bucket.MaxListReadQuotaNumber
rateCfg := makeAPIRateLimitCfg(cfg.APIRateLimiter)
if err := mwhttp.NewAPILimiter(rateCfg); err != nil {
log.Errorw("failed to new api limiter", "err", err)
return err
}
return nil
}
func makeAPIRateLimitCfg(cfg mwhttp.RateLimiterConfig) *mwhttp.APILimiterConfig {
pathPatternMap := make(map[string][]mwhttp.MemoryLimiterConfig)
pathSequence := make([]string, len(cfg.PathPattern))
// for each name inside all PathPattern, try match with NameToLimit and get its pairing RateLimit and RatePeriod
for i, c := range cfg.PathPattern {
for _, name := range c.Names {
for _, v := range cfg.NameToLimit {
if strings.EqualFold(v.Name, name) {
// pass in method info along with path
pathWithMethod := c.Method + "-" + c.Key
pathSequence[i] = pathWithMethod
pathPatternMap[pathWithMethod] = append(pathPatternMap[pathWithMethod], v)
}
}
}
}
patternMap := make(map[string][]mwhttp.MemoryLimiterConfig)
hostSequence := make([]string, len(cfg.HostPattern))
// for each name inside all HostPattern, try match with NameToLimit and get its pairing RateLimit and RatePeriod
for i, c := range cfg.HostPattern {
for _, name := range c.Names {
for _, v := range cfg.NameToLimit {
if strings.EqualFold(v.Name, name) {
hostSequence[i] = c.Key
patternMap[c.Key] = append(patternMap[c.Key], v)
}
}
}
}
apiLimitsMap := make(map[string][]mwhttp.MemoryLimiterConfig)
// for each name inside all APILimits, try match with NameToLimit and get its pairing RateLimit and RatePeriod
for _, c := range cfg.APILimits {
for _, name := range c.Names {
for _, v := range cfg.NameToLimit {
if strings.EqualFold(v.Name, name) {
apiLimitsMap[c.Key] = append(apiLimitsMap[c.Key], v)
}
}
}
}
return &mwhttp.APILimiterConfig{
PathPattern: pathPatternMap,
PathSequence: pathSequence,
HostPattern: patternMap,
HostSequence: hostSequence,
APILimits: apiLimitsMap,
IPLimitCfg: cfg.IPLimitCfg,
}
}