-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
183 lines (155 loc) · 3.58 KB
/
interface.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package mutex
import (
"fmt"
"log"
"time"
. "github.com/infrago/base"
"github.com/infrago/infra"
"github.com/infrago/util"
)
func (this *Module) Register(name string, value Any) {
switch config := value.(type) {
case Driver:
this.Driver(name, config)
case Config:
this.Config(name, config)
case Configs:
this.Configs(config)
}
}
func (this *Module) configure(name string, config Map) {
cfg := Config{
Driver: DEFAULT, Weight: 1, Expiry: time.Second,
}
//如果已经存在了,用现成的改写
if vv, ok := this.configs[name]; ok {
cfg = vv
}
if driver, ok := config["driver"].(string); ok {
cfg.Driver = driver
}
//分配权重
if weight, ok := config["weight"].(int); ok {
cfg.Weight = weight
}
if weight, ok := config["weight"].(int64); ok {
cfg.Weight = int(weight)
}
if weight, ok := config["weight"].(float64); ok {
cfg.Weight = int(weight)
}
//默认过期时间,单位秒
if expiry, ok := config["expiry"].(string); ok {
dur, err := util.ParseDuration(expiry)
if err == nil {
cfg.Expiry = dur
}
}
if expiry, ok := config["expiry"].(int); ok {
cfg.Expiry = time.Second * time.Duration(expiry)
}
if expiry, ok := config["expiry"].(float64); ok {
cfg.Expiry = time.Second * time.Duration(expiry)
}
if setting, ok := config["setting"].(Map); ok {
cfg.Setting = setting
}
//保存配置
this.configs[name] = cfg
}
func (this *Module) Configure(global Map) {
var config Map
if vvv, ok := global["mutex"].(Map); ok {
config = vvv
}
if config == nil {
return
}
//记录上一层的配置,如果有的话
rootConfig := Map{}
for key, val := range config {
if conf, ok := val.(Map); ok {
this.configure(key, conf)
} else {
rootConfig[key] = val
}
}
if len(rootConfig) > 0 {
this.configure(infra.DEFAULT, rootConfig)
}
}
func (this *Module) Initialize() {
if this.initialized {
return
}
// 如果没有配置任何连接时,默认一个
if len(this.configs) == 0 {
this.configs[infra.DEFAULT] = Config{
Driver: infra.DEFAULT, Weight: 1, Expiry: time.Second,
}
} else {
//session全部分布,不留单独外部连接
for key, config := range this.configs {
if config.Weight == 0 {
config.Weight = 1
}
if config.Expiry == 0 {
config.Expiry = time.Second
}
this.configs[key] = config
}
}
this.initialized = true
}
func (this *Module) Connect() {
if this.connected {
return
}
//记录要参与分布的连接和权重
weights := make(map[string]int)
for name, config := range this.configs {
driver, ok := this.drivers[config.Driver]
if ok == false {
panic("Invalid mutex driver: " + config.Driver)
}
inst := &Instance{
nil, name, config, config.Setting,
}
// 建立连接
connect, err := driver.Connect(inst)
if err != nil {
panic("Failed to connect to mutex: " + err.Error())
}
// 打开连接
err = connect.Open()
if err != nil {
panic("Failed to open mutex connect: " + err.Error())
}
inst.connect = connect
//保存实例
this.instances[name] = inst
//只有设置了权重的才参与分布
if config.Weight > 0 {
weights[name] = config.Weight
}
}
//hashring分片
this.weights = weights
this.hashring = util.NewHashRing(weights)
this.connected = true
}
func (this *Module) Launch() {
if this.launched {
return
}
log.Println(fmt.Sprintf("%s MUTEX is running with %d connects.", infra.INFRAGO, len(this.instances)))
this.launched = true
}
func (this *Module) Terminate() {
for _, ins := range this.instances {
ins.connect.Close()
}
this.launched = false
this.connected = false
this.initialized = false
}