-
Notifications
You must be signed in to change notification settings - Fork 32
/
Main.go
113 lines (90 loc) · 2.69 KB
/
Main.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
package switcherapiserver
import (
"encoding/json"
"io/ioutil"
"sync"
"time"
"github.com/golang/glog"
"github.com/samuel/go-zookeeper/zk"
)
// Zookeeper连接超时时间
const zookeeperConnTimeout = 5
// ConfigData 配置数据
type ConfigData struct {
// 是否启用 API Server
EnableAPIServer bool
// API 用户名
APIUser string
// API 密码
APIPassword string
// API Server 的监听IP:端口
ListenAddr string
// AvailableCoins 可用币种,形如 {"btc", "bcc", ...}
AvailableCoins []string
// Zookeeper集群的IP:端口列表
ZKBroker []string
// ZKSwitcherWatchDir Switcher监控的Zookeeper路径,以斜杠结尾
ZKSwitcherWatchDir string
// 是否启用定时检测任务
EnableCronJob bool
// 定时检测间隔时间
CronIntervalSeconds int
// 用户:币种对应表的URL
UserCoinMapURL string
// 挖矿服务器对子账户名大小写不敏感,此时将总是写入小写的子账户名
StratumServerCaseInsensitive bool
//子池更新用的zookeeper根目录(注意,不应包括币种和子池名称),以斜杠结尾
ZKSubPoolUpdateBaseDir string
// 子池更新时jobmaker的应答超时时间,如果在该时间内jobmaker没有应答,则API返回错误
ZKSubPoolUpdateAckTimeout int
}
// zookeeperConn Zookeeper连接对象
var zookeeperConn *zk.Conn
// 配置数据
var configData *ConfigData
// 用于等待goroutine结束
var waitGroup sync.WaitGroup
// Main function
func Main(configFilePath string) {
// 读取配置文件
configJSON, err := ioutil.ReadFile(configFilePath)
if err != nil {
glog.Fatal("read config failed: ", err)
return
}
configData = new(ConfigData)
err = json.Unmarshal(configJSON, configData)
if err != nil {
glog.Fatal("parse config failed: ", err)
return
}
// 若zookeeper路径不以“/”结尾,则添加
if configData.ZKSwitcherWatchDir[len(configData.ZKSwitcherWatchDir)-1] != '/' {
configData.ZKSwitcherWatchDir += "/"
}
if len(configData.ZKSubPoolUpdateBaseDir) > 0 && configData.ZKSubPoolUpdateBaseDir[len(configData.ZKSubPoolUpdateBaseDir)-1] != '/' {
configData.ZKSubPoolUpdateBaseDir += "/"
}
// 建立到Zookeeper集群的连接
conn, _, err := zk.Connect(configData.ZKBroker, time.Duration(zookeeperConnTimeout)*time.Second)
if err != nil {
glog.Fatal("Connect Zookeeper Failed: ", err)
return
}
zookeeperConn = conn
// 检查并创建StratumSwitcher使用的Zookeeper路径
err = createZookeeperPath(configData.ZKSwitcherWatchDir)
if err != nil {
glog.Fatal("Create Zookeeper Path Failed: ", err)
return
}
if configData.EnableAPIServer {
waitGroup.Add(1)
go runAPIServer()
}
if configData.EnableCronJob {
waitGroup.Add(1)
go RunCronJob()
}
waitGroup.Wait()
}