forked from Terry-Mao/gosnowflake
/
config.go
97 lines (87 loc) · 2.89 KB
/
config.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
// Copyright © 2014 Terry Mao, LiuDing All rights reserved.
// This file is part of gosnowflake.
// gosnowflake is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gosnowflake is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gosnowflake. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"flag"
"github.com/Terry-Mao/goconf"
"github.com/golang/glog"
"runtime"
"time"
)
var (
// global config object
goConf = goconf.New()
MyConf *Config
confPath string
)
type Config struct {
User string `goconf:"base:user"`
PidFile string `goconf:"base:pid"`
Dir string `goconf:"base:dir"`
MaxProc int `goconf:"base:maxproc"`
RPCBind []string `goconf:"base:rpc.bind:,"`
StatBind []string `goconf:"base:stat.bind:,"`
PprofBind []string `goconf:"base:pprof.bind:,"`
DatacenterId int64 `goconf:"snowflake:datacenter"`
WorkerId []int64 `goconf:"snowflake:worker"`
ZKAddr []string `goconf:"zookeeper:addr"`
ZKTimeout time.Duration `goconf:"zookeeper:timeout:time`
ZKPath string `goconf:"zookeeper:path"`
}
func init() {
flag.StringVar(&confPath, "conf", "./gosnowflake.conf", " set gosnowflake config file path")
}
// Init init the configuration file.
func InitConfig() error {
MyConf = &Config{
User: "nobody",
PidFile: "/tmp/user_account.pid",
Dir: "/dev/null",
MaxProc: runtime.NumCPU(),
RPCBind: []string{"localhost:8080"},
DatacenterId: 0,
WorkerId: []int64{0},
ZKAddr: []string{"localhost:2181"},
ZKTimeout: time.Second * 15,
ZKPath: "/gosnowflake-servers",
}
if err := goConf.Parse(confPath); err != nil {
glog.Errorf("goconf.Parse(\"%s\") error(%v)", confPath, err)
return err
}
if err := goConf.Unmarshal(MyConf); err != nil {
glog.Errorf("goconf.Unmarshall() error(%v)", err)
return err
}
return nil
}
// Reload reload the configuration file.
func ReloadConfig() error {
glog.Infof("config file: \"%s\" reload", confPath)
t, err := goConf.Reload()
if err != nil {
glog.Errorf("goconf.Reload() error(%v)", err)
glog.Warningf("confi file: \"%s\" reload failed, use original one", confPath)
return err
}
goConf = t
// new a Config for Unmarshal
myConf := &Config{}
if err := goConf.Unmarshal(myConf); err != nil {
glog.Errorf("goconf.Unmarshall() error(%v)", err)
return err
}
// atomic replace MyConf
MyConf = myConf
return nil
}