Skip to content

Commit 3866192

Browse files
authored
Merge pull request #33 from gitbitex/revert-18-master
Revert "change the manner we get the configure."
2 parents 351d9e9 + 7fecc3a commit 3866192

15 files changed

+65
-48
lines changed

conf/gbe_config.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package conf
1717
import (
1818
"encoding/json"
1919
"io/ioutil"
20+
"sync"
2021
)
2122

2223
type GbeConfig struct {
@@ -55,16 +56,20 @@ type RestServerConfig struct {
5556
Addr string `json:"addr"`
5657
}
5758

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

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-
}
69-
}
62+
func GetConfig() *GbeConfig {
63+
configOnce.Do(func() {
64+
bytes, err := ioutil.ReadFile("conf.json")
65+
if err != nil {
66+
panic(err)
67+
}
7068

69+
err = json.Unmarshal(bytes, &config)
70+
if err != nil {
71+
panic(err)
72+
}
73+
})
74+
return &config
75+
}

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2 h1:wBORZD4gvEKK0tG
188188
github.com/pingcap/kvproto v0.0.0-20190904075355-9a1bd6a31da2/go.mod h1:QMdbTAXCHzzygQzqcG9uVUgU2fKeSN1GmfMiykdSzzY=
189189
github.com/pingcap/parser v0.0.0-20190506092653-e336082eb825 h1:U9Kdnknj4n2v76Mg7wazevZ5N9U1OIaMwSNRVLEcLX0=
190190
github.com/pingcap/parser v0.0.0-20190506092653-e336082eb825/go.mod h1:1FNvfp9+J0wvc4kl8eGNh7Rqrxveg15jJoWo/a0uHwA=
191-
github.com/pingcap/pd v2.1.18+incompatible h1:ytgHQQlEwvuYWN6gKjivUq8QOL7TIeaPfb5kapaaFNo=
192191
github.com/pingcap/tidb v2.0.11+incompatible h1:Shz+ry1DzQNsPk1QAejnM+5tgjbwZuzPnIER5aCjQ6c=
193192
github.com/pingcap/tidb v2.0.11+incompatible/go.mod h1:I8C6jrPINP2rrVunTRd7C9fRRhQrtR43S1/CL5ix/yQ=
194193
github.com/pingcap/tipb v0.0.0-20190428032612-535e1abaa330 h1:rRMLMjIMFulCX9sGKZ1hoov/iROMsKyC8Snc02nSukw=

main.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
package main
1616

1717
import (
18-
"net/http"
19-
_ "net/http/pprof"
20-
2118
"github.com/gitbitex/gitbitex-spot/conf"
2219
"github.com/gitbitex/gitbitex-spot/matching"
2320
"github.com/gitbitex/gitbitex-spot/models"
@@ -26,11 +23,12 @@ import (
2623
"github.com/gitbitex/gitbitex-spot/service"
2724
"github.com/gitbitex/gitbitex-spot/worker"
2825
"github.com/prometheus/common/log"
26+
"net/http"
27+
_ "net/http/pprof"
2928
)
3029

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

3533
go func() {
3634
log.Info(http.ListenAndServe("localhost:6060", nil))
@@ -49,9 +47,9 @@ func main() {
4947
panic(err)
5048
}
5149
for _, product := range products {
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()
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()
5553
}
5654

5755
rest.StartServer()

matching/bootstrap.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@ import (
2121
)
2222

2323
func StartEngine() {
24+
gbeConfig := conf.GetConfig()
25+
2426
products, err := service.GetProducts()
2527
if err != nil {
2628
panic(err)
2729
}
2830
for _, product := range products {
29-
orderReader := NewKafkaOrderReader(product.Id, conf.Config.Kafka.Brokers)
31+
orderReader := NewKafkaOrderReader(product.Id, gbeConfig.Kafka.Brokers)
3032
snapshotStore := NewRedisSnapshotStore(product.Id)
31-
logStore := NewKafkaLogStore(product.Id, conf.Config.Kafka.Brokers)
33+
logStore := NewKafkaLogStore(product.Id, gbeConfig.Kafka.Brokers)
3234
matchEngine := NewEngine(product, orderReader, logStore, snapshotStore)
3335
matchEngine.Start()
3436
}

matching/redis_snapshot_store.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ type RedisSnapshotStore struct {
3131
}
3232

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

3536
redisClient := redis.NewClient(&redis.Options{
36-
Addr: conf.Config.Redis.Addr,
37-
Password: conf.Config.Redis.Password,
37+
Addr: gbeConfig.Redis.Addr,
38+
Password: gbeConfig.Redis.Password,
3839
DB: 0,
3940
})
4041

models/binlog_stream.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ type BinLogStream struct {
3232
}
3333

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

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

158160
func (s *BinLogStream) Start() {
161+
gbeConfig := conf.GetConfig()
159162

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

models/mysql/store.go

+1-1
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.Config
53+
cfg := conf.GetConfig()
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
)
2323

2424
func StartServer() {
25+
gbeConfig := conf.GetConfig()
2526

2627
sub := newSubscription()
2728

@@ -32,12 +33,12 @@ func StartServer() {
3233
panic(err)
3334
}
3435
for _, product := range products {
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()
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()
3839
}
3940

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

4243
log.Info("websocket server ok")
4344
}

pushing/order_book.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,11 @@ var onceStore sync.Once
202202

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

206207
redisClient := redis.NewClient(&redis.Options{
207-
Addr: conf.Config.Redis.Addr,
208-
Password: conf.Config.Redis.Password,
208+
Addr: gbeConfig.Redis.Addr,
209+
Password: gbeConfig.Redis.Password,
209210
DB: 0,
210211
})
211212

pushing/redis_stream.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ func newRedisStream(sub *subscription) *redisStream {
3838
}
3939

4040
func (s *redisStream) Start() {
41+
gbeConfig := conf.GetConfig()
4142

4243
redisClient := redis.NewClient(&redis.Options{
43-
Addr: conf.Config.Redis.Addr,
44-
Password: conf.Config.Redis.Password,
44+
Addr: gbeConfig.Redis.Addr,
45+
Password: gbeConfig.Redis.Password,
4546
DB: 0,
4647
})
4748
_, err := redisClient.Ping().Result()

rest/bootstrap.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ import (
2020
)
2121

2222
func StartServer() {
23+
gbeConfig := conf.GetConfig()
2324

24-
httpServer := NewHttpServer(conf.Config.RestServer.Addr)
25+
httpServer := NewHttpServer(gbeConfig.RestServer.Addr)
2526
go httpServer.Start()
2627

2728
log.Info("rest server ok")

rest/order_controller.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ func getWriter(productId string) *kafka.Writer {
4444
return writer.(*kafka.Writer)
4545
}
4646

47+
gbeConfig := conf.GetConfig()
48+
4749
newWriter := kafka.NewWriter(kafka.WriterConfig{
48-
Brokers: conf.Config.Kafka.Brokers,
50+
Brokers: gbeConfig.Kafka.Brokers,
4951
Topic: matching.TopicOrderPrefix + productId,
5052
Balancer: &kafka.LeastBytes{},
5153
BatchTimeout: 5 * time.Millisecond,

service/user_service.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ func RefreshAccessToken(email, password string) (string, error) {
6363
"expiredAt": time.Now().Unix(),
6464
}
6565
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim)
66-
return token.SignedString([]byte(conf.Config.JwtSecret))
66+
return token.SignedString([]byte(conf.GetConfig().JwtSecret))
6767
}
6868

6969
func CheckToken(tokenStr string) (*models.User, error) {
7070
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
71-
return []byte(conf.Config.JwtSecret), nil
71+
return []byte(conf.GetConfig().JwtSecret), nil
7272
})
7373
if err != nil {
7474
return nil, err

worker/bill_executor.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ func (s *BillExecutor) Start() {
5757
}
5858

5959
func (s *BillExecutor) runMqListener() {
60+
gbeConfig := conf.GetConfig()
61+
6062
redisClient := redis.NewClient(&redis.Options{
61-
Addr: conf.Config.Redis.Addr,
62-
Password: conf.Config.Redis.Password,
63+
Addr: gbeConfig.Redis.Addr,
64+
Password: gbeConfig.Redis.Password,
6365
DB: 0,
6466
})
6567

worker/fill_executor.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ func (s *FillExecutor) Start() {
8585

8686
// 监听消息队列通知
8787
func (s *FillExecutor) runMqListener() {
88+
gbeConfig := conf.GetConfig()
8889

8990
redisClient := redis.NewClient(&redis.Options{
90-
Addr: conf.Config.Redis.Addr,
91-
Password: conf.Config.Redis.Password,
91+
Addr: gbeConfig.Redis.Addr,
92+
Password: gbeConfig.Redis.Password,
9293
DB: 0,
9394
})
9495

0 commit comments

Comments
 (0)