Skip to content

Commit b780016

Browse files
author
pioneerlfn
committed
Add a global variable in conf pakage to avoid too many function call in order to get the configure.
1 parent 8f83a76 commit b780016

16 files changed

+51
-65
lines changed

conf/gbe_config.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package conf
1717
import (
1818
"encoding/json"
1919
"io/ioutil"
20-
"sync"
2120
)
2221

2322
type GbeConfig struct {
@@ -56,20 +55,16 @@ type RestServerConfig struct {
5655
Addr string `json:"addr"`
5756
}
5857

59-
var config GbeConfig
60-
var configOnce sync.Once
58+
var Config *GbeConfig
6159

62-
func GetConfig() *GbeConfig {
63-
configOnce.Do(func() {
64-
bytes, err := ioutil.ReadFile("conf.json")
65-
if err != nil {
66-
panic(err)
67-
}
68-
69-
err = json.Unmarshal(bytes, &config)
70-
if err != nil {
71-
panic(err)
72-
}
73-
})
74-
return &config
60+
func Init() {
61+
bytes, err := ioutil.ReadFile("conf.json")
62+
if err != nil {
63+
panic(err)
64+
}
65+
err = json.Unmarshal(bytes, &Config)
66+
if err != nil {
67+
panic(err)
68+
}
7569
}
70+

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,6 @@ require (
6161
go.uber.org/zap v1.10.0 // indirect
6262
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce // indirect
6363
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
64+
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
6465
sigs.k8s.io/yaml v1.1.0 // indirect
6566
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2 h1:wBORZD4gvEKK0tG
187187
github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2/go.mod h1:QMdbTAXCHzzygQzqcG9uVUgU2fKeSN1GmfMiykdSzzY=
188188
github.com/pingcap/parser v0.0.0-20190506092653-e336082eb825 h1:U9Kdnknj4n2v76Mg7wazevZ5N9U1OIaMwSNRVLEcLX0=
189189
github.com/pingcap/parser v0.0.0-20190506092653-e336082eb825/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
190+
github.com/pingcap/pd v2.1.18+incompatible h1:ytgHQQlEwvuYWN6gKjivUq8QOL7TIeaPfb5kapaaFNo=
190191
github.com/pingcap/tidb v2.0.11+incompatible h1:Shz+ry1DzQNsPk1QAejnM+5tgjbwZuzPnIER5aCjQ6c=
191192
github.com/pingcap/tidb v2.0.11+incompatible/go.mod h1:I8C6jrPINP2rrVunTRd7C9fRRhQrtR43S1/CL5ix/yQ=
192193
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330 h1:rRMLMjIMFulCX9sGKZ1hoov/iROMsKyC8Snc02nSukw=
@@ -348,6 +349,8 @@ gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3M
348349
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
349350
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
350351
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
352+
gopkg.in/stretchr/testify.v1 v1.2.2 h1:yhQC6Uy5CqibAIlk1wlusa/MJ3iAN49/BsR/dCCKz3M=
353+
gopkg.in/stretchr/testify.v1 v1.2.2/go.mod h1:QI5V/q6UbPmuhtm10CaFZxED9NreB8PnFYN9JcR6TxU=
351354
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
352355
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
353356
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=

main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
package main
1616

1717
import (
18+
"net/http"
19+
_ "net/http/pprof"
20+
1821
"github.com/gitbitex/gitbitex-spot/conf"
1922
"github.com/gitbitex/gitbitex-spot/matching"
2023
"github.com/gitbitex/gitbitex-spot/models"
@@ -23,12 +26,11 @@ import (
2326
"github.com/gitbitex/gitbitex-spot/service"
2427
"github.com/gitbitex/gitbitex-spot/worker"
2528
"github.com/prometheus/common/log"
26-
"net/http"
27-
_ "net/http/pprof"
2829
)
2930

3031
func main() {
31-
gbeConfig := conf.GetConfig()
32+
// gbeConfig := conf.GetConfig()
33+
conf.Init()
3234

3335
go func() {
3436
log.Info(http.ListenAndServe("localhost:6060", nil))
@@ -47,9 +49,9 @@ func main() {
4749
panic(err)
4850
}
4951
for _, product := range products {
50-
worker.NewTickMaker(product.Id, matching.NewKafkaLogReader("tickMaker", product.Id, gbeConfig.Kafka.Brokers)).Start()
51-
worker.NewFillMaker(matching.NewKafkaLogReader("fillMaker", product.Id, gbeConfig.Kafka.Brokers)).Start()
52-
worker.NewTradeMaker(matching.NewKafkaLogReader("tradeMaker", product.Id, gbeConfig.Kafka.Brokers)).Start()
52+
worker.NewTickMaker(product.Id, matching.NewKafkaLogReader("tickMaker", product.Id, conf.Config.Kafka.Brokers)).Start()
53+
worker.NewFillMaker(matching.NewKafkaLogReader("fillMaker", product.Id, conf.Config.Kafka.Brokers)).Start()
54+
worker.NewTradeMaker(matching.NewKafkaLogReader("tradeMaker", product.Id, conf.Config.Kafka.Brokers)).Start()
5355
}
5456

5557
rest.StartServer()

matching/bootstrap.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ import (
2121
)
2222

2323
func StartEngine() {
24-
gbeConfig := conf.GetConfig()
25-
2624
products, err := service.GetProducts()
2725
if err != nil {
2826
panic(err)
2927
}
3028
for _, product := range products {
31-
orderReader := NewKafkaOrderReader(product.Id, gbeConfig.Kafka.Brokers)
29+
orderReader := NewKafkaOrderReader(product.Id, conf.Config.Kafka.Brokers)
3230
snapshotStore := NewRedisSnapshotStore(product.Id)
33-
logStore := NewKafkaLogStore(product.Id, gbeConfig.Kafka.Brokers)
31+
logStore := NewKafkaLogStore(product.Id, conf.Config.Kafka.Brokers)
3432
matchEngine := NewEngine(product, orderReader, logStore, snapshotStore)
3533
matchEngine.Start()
3634
}

matching/redis_snapshot_store.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ type RedisSnapshotStore struct {
3131
}
3232

3333
func NewRedisSnapshotStore(productId string) SnapshotStore {
34-
gbeConfig := conf.GetConfig()
3534

3635
redisClient := redis.NewClient(&redis.Options{
37-
Addr: gbeConfig.Redis.Addr,
38-
Password: gbeConfig.Redis.Password,
36+
Addr: conf.Config.Redis.Addr,
37+
Password: conf.Config.Redis.Password,
3938
DB: 0,
4039
})
4140

models/binlog_stream.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,9 @@ type BinLogStream struct {
3232
}
3333

3434
func NewBinLogStream() *BinLogStream {
35-
gbeConfig := conf.GetConfig()
36-
3735
redisClient := redis.NewClient(&redis.Options{
38-
Addr: gbeConfig.Redis.Addr,
39-
Password: gbeConfig.Redis.Password,
36+
Addr: conf.Config.Redis.Addr,
37+
Password: conf.Config.Redis.Password,
4038
DB: 0,
4139
})
4240

@@ -158,16 +156,15 @@ func (s *BinLogStream) getColumnIndexByName(e *canal.RowsEvent, name string) int
158156
}
159157

160158
func (s *BinLogStream) Start() {
161-
gbeConfig := conf.GetConfig()
162159

163160
cfg := canal.NewDefaultConfig()
164-
cfg.Addr = gbeConfig.DataSource.Addr
165-
cfg.User = gbeConfig.DataSource.User
166-
cfg.Password = gbeConfig.DataSource.Password
161+
cfg.Addr = conf.Config.DataSource.Addr
162+
cfg.User = conf.Config.DataSource.User
163+
cfg.Password = conf.Config.DataSource.Password
167164
cfg.Dump.ExecutionPath = ""
168-
cfg.Dump.TableDB = gbeConfig.DataSource.Database
165+
cfg.Dump.TableDB = conf.Config.DataSource.Database
169166
cfg.ParseTime = true
170-
cfg.IncludeTableRegex = []string{gbeConfig.DataSource.Database + "\\..*"}
167+
cfg.IncludeTableRegex = []string{conf.Config.DataSource.Database + "\\..*"}
171168
cfg.ExcludeTableRegex = []string{"mysql\\..*"}
172169
c, err := canal.NewCanal(cfg)
173170
if err != nil {

models/mysql/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewStore(db *gorm.DB) *Store {
5050
}
5151

5252
func initDb() error {
53-
cfg := conf.GetConfig()
53+
cfg := conf.Config
5454

5555
url := fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=Local",
5656
cfg.DataSource.User, cfg.DataSource.Password, cfg.DataSource.Addr, cfg.DataSource.Database)

pushing/bootstrap.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
)
2323

2424
func StartServer() {
25-
gbeConfig := conf.GetConfig()
2625

2726
sub := newSubscription()
2827

@@ -33,12 +32,12 @@ func StartServer() {
3332
panic(err)
3433
}
3534
for _, product := range products {
36-
newTickerStream(product.Id, sub, matching.NewKafkaLogReader("tickerStream", product.Id, gbeConfig.Kafka.Brokers)).Start()
37-
newMatchStream(product.Id, sub, matching.NewKafkaLogReader("matchStream", product.Id, gbeConfig.Kafka.Brokers)).Start()
38-
newOrderBookStream(product.Id, sub, matching.NewKafkaLogReader("orderBookStream", product.Id, gbeConfig.Kafka.Brokers)).Start()
35+
newTickerStream(product.Id, sub, matching.NewKafkaLogReader("tickerStream", product.Id, conf.Config.Kafka.Brokers)).Start()
36+
newMatchStream(product.Id, sub, matching.NewKafkaLogReader("matchStream", product.Id, conf.Config.Kafka.Brokers)).Start()
37+
newOrderBookStream(product.Id, sub, matching.NewKafkaLogReader("orderBookStream", product.Id, conf.Config.Kafka.Brokers)).Start()
3938
}
4039

41-
go NewServer(gbeConfig.PushServer.Addr, gbeConfig.PushServer.Path, sub).Run()
40+
go NewServer(conf.Config.PushServer.Addr, conf.Config.PushServer.Path, sub).Run()
4241

4342
log.Info("websocket server ok")
4443
}

pushing/order_book.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,10 @@ var onceStore sync.Once
202202

203203
func sharedSnapshotStore() *redisSnapshotStore {
204204
onceStore.Do(func() {
205-
gbeConfig := conf.GetConfig()
206205

207206
redisClient := redis.NewClient(&redis.Options{
208-
Addr: gbeConfig.Redis.Addr,
209-
Password: gbeConfig.Redis.Password,
207+
Addr: conf.Config.Redis.Addr,
208+
Password: conf.Config.Redis.Password,
210209
DB: 0,
211210
})
212211

0 commit comments

Comments
 (0)