-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_grpc.go
72 lines (61 loc) · 1.61 KB
/
rule_grpc.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
package rproxy
import (
"fmt"
"sync"
)
// GRPCRule defines the rule structure
type GRPCRule struct {
ServicePrefix, ForwardDst string
Insecure bool
}
// GRPCRuleMgr ...
type GRPCRuleMgr struct {
sync.Map
}
// GRPC global rule manager ...
var GRPC *GRPCRuleMgr
func init() {
GRPC = &GRPCRuleMgr{}
}
// CreateOrUpdateRule creates or update rule for uri prefix
// servicePrefix is like /proto.TestService/xxxxx
// forwardDst is the full access address the request will be forward to
func (h *GRPCRuleMgr) CreateOrUpdateRule(servicePrefix, forwardDst string, insecure bool) {
h.Store(servicePrefix, &GRPCRule{
ServicePrefix: servicePrefix,
ForwardDst: forwardDst,
Insecure: insecure})
}
// RemoveRule removes rule for prefix
func (h *GRPCRuleMgr) RemoveRule(uriPrefix string) error {
if _, ok := h.Load(uriPrefix); ok {
h.Delete(ok)
return nil
}
return fmt.Errorf("unknown uri prefix: " + uriPrefix)
}
// ClearRules removes all rules
func (h *GRPCRuleMgr) ClearRules() {
h.Map = sync.Map{}
}
// ListRules lists all rules
func (h *GRPCRuleMgr) ListRules() []GRPCRule {
rules := []GRPCRule{}
h.Range(func(k, v interface{}) bool {
rule := v.(*GRPCRule)
rules = append(rules, *rule)
return true
})
return rules
}
func (h *GRPCRuleMgr) getRule(uriPrefix string) (rule *GRPCRule, err error) {
if v, ok := h.Load(uriPrefix); ok {
return v.(*GRPCRule), nil
}
return nil, fmt.Errorf("unknown uri prefix: " + uriPrefix)
}
func (h *GRPCRuleMgr) rangeRule(f func(servicePrefix string, rule *GRPCRule) bool) {
h.Range(func(k, v interface{}) bool {
return f(k.(string), v.(*GRPCRule))
})
}