/
namespace.go
112 lines (90 loc) · 2.63 KB
/
namespace.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
package namespace
import (
"context"
"fmt"
"github.com/pingcap/errors"
"github.com/tidb-incubator/weir/pkg/config"
"github.com/tidb-incubator/weir/pkg/proxy/driver"
"github.com/tidb-incubator/weir/pkg/proxy/metrics"
)
type NamespaceHolder struct {
nss map[string]Namespace
}
type NamespaceWrapper struct {
nsmgr *NamespaceManager
name string
}
func CreateNamespaceHolder(cfgs []*config.Namespace, build NamespaceBuilder) (*NamespaceHolder, error) {
nss := make(map[string]Namespace, len(cfgs))
for _, cfg := range cfgs {
ns, err := build(cfg)
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("create namespace error, namespace: %s", cfg.Namespace))
}
nss[cfg.Namespace] = ns
}
holder := &NamespaceHolder{
nss: nss,
}
return holder, nil
}
func (n *NamespaceHolder) Get(name string) (Namespace, bool) {
ns, ok := n.nss[name]
return ns, ok
}
func (n *NamespaceHolder) Set(name string, ns Namespace) {
n.nss[name] = ns
}
func (n *NamespaceHolder) Delete(name string) {
delete(n.nss, name)
}
func (n *NamespaceHolder) Clone() *NamespaceHolder {
nss := make(map[string]Namespace)
for name, ns := range n.nss {
nss[name] = ns
}
return &NamespaceHolder{
nss: nss,
}
}
func (n *NamespaceWrapper) Name() string {
return n.name
}
func (n *NamespaceWrapper) IsDatabaseAllowed(db string) bool {
return n.mustGetCurrentNamespace().IsDatabaseAllowed(db)
}
func (n *NamespaceWrapper) ListDatabases() []string {
return n.mustGetCurrentNamespace().ListDatabases()
}
func (n *NamespaceWrapper) IsDeniedSQL(sqlFeature uint32) bool {
return n.mustGetCurrentNamespace().IsDeniedSQL(sqlFeature)
}
func (n *NamespaceWrapper) IsAllowedSQL(sqlFeature uint32) bool {
return n.mustGetCurrentNamespace().IsAllowedSQL(sqlFeature)
}
func (n *NamespaceWrapper) GetPooledConn(ctx context.Context) (driver.PooledBackendConn, error) {
return n.mustGetCurrentNamespace().GetPooledConn(ctx)
}
func (n *NamespaceWrapper) IncrConnCount() {
metrics.QueryCtxGauge.WithLabelValues(n.name).Inc()
}
func (n *NamespaceWrapper) DescConnCount() {
metrics.QueryCtxGauge.WithLabelValues(n.name).Dec()
}
func (n *NamespaceWrapper) Closed() bool {
_, ok := n.nsmgr.getCurrentNamespaces().Get(n.name)
return !ok
}
func (n *NamespaceWrapper) GetBreaker() (driver.Breaker, error) {
return n.mustGetCurrentNamespace().GetBreaker()
}
func (n *NamespaceWrapper) GetRateLimiter() driver.RateLimiter {
return n.mustGetCurrentNamespace().GetRateLimiter()
}
func (n *NamespaceWrapper) mustGetCurrentNamespace() Namespace {
ns, ok := n.nsmgr.getCurrentNamespaces().Get(n.name)
if !ok {
panic(errors.New("namespace not found"))
}
return ns
}