Skip to content

Commit 7642513

Browse files
author
greensheng
committed
fix bug:too many open files
1 parent 938d4a2 commit 7642513

File tree

14 files changed

+38
-82
lines changed

14 files changed

+38
-82
lines changed

conf/gbe_config.go

Lines changed: 16 additions & 11 deletions
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-
func GetConfig() (*GbeConfig, error) {
59-
bytes, err := ioutil.ReadFile("conf.json")
60-
if err != nil {
61-
return nil, err
62-
}
59+
var config GbeConfig
60+
var configOnce sync.Once
6361

64-
var config GbeConfig
65-
err = json.Unmarshal(bytes, &config)
66-
if err != nil {
67-
return nil, err
68-
}
69-
return &config, nil
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
7075
}

main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ import (
2828
)
2929

3030
func main() {
31-
gbeConfig, err := conf.GetConfig()
32-
if err != nil {
33-
panic(err)
34-
}
31+
gbeConfig := conf.GetConfig()
3532

3633
go func() {
3734
log.Info(http.ListenAndServe("localhost:6060", nil))

matching/bootstrap.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ import (
2121
)
2222

2323
func StartEngine() {
24-
gbeConfig, err := conf.GetConfig()
25-
if err != nil {
26-
panic(err)
27-
}
24+
gbeConfig := conf.GetConfig()
2825

2926
products, err := service.GetProducts()
3027
if err != nil {

matching/redis_snapshot_store.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ type RedisSnapshotStore struct {
3131
}
3232

3333
func NewRedisSnapshotStore(productId string) SnapshotStore {
34-
gbeConfig, err := conf.GetConfig()
35-
if err != nil {
36-
panic(err)
37-
}
34+
gbeConfig := conf.GetConfig()
3835

3936
redisClient := redis.NewClient(&redis.Options{
4037
Addr: gbeConfig.Redis.Addr,

models/binlog_stream.go

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

3434
func NewBinLogStream() *BinLogStream {
35-
gbeConfig, err := conf.GetConfig()
36-
if err != nil {
37-
panic(err)
38-
}
35+
gbeConfig := conf.GetConfig()
3936

4037
redisClient := redis.NewClient(&redis.Options{
4138
Addr: gbeConfig.Redis.Addr,
@@ -161,10 +158,7 @@ func (s *BinLogStream) getColumnIndexByName(e *canal.RowsEvent, name string) int
161158
}
162159

163160
func (s *BinLogStream) Start() {
164-
gbeConfig, err := conf.GetConfig()
165-
if err != nil {
166-
panic(err)
167-
}
161+
gbeConfig := conf.GetConfig()
168162

169163
cfg := canal.NewDefaultConfig()
170164
cfg.Addr = gbeConfig.DataSource.Addr

models/mysql/store.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ func NewStore(db *gorm.DB) *Store {
5050
}
5151

5252
func initDb() error {
53-
cfg, err := conf.GetConfig()
54-
if err != nil {
55-
return err
56-
}
53+
cfg := conf.GetConfig()
5754

5855
url := fmt.Sprintf("%v:%v@tcp(%v)/%v?charset=utf8&parseTime=True&loc=Local",
5956
cfg.DataSource.User, cfg.DataSource.Password, cfg.DataSource.Addr, cfg.DataSource.Database)
57+
var err error
6058
gdb, err = gorm.Open(cfg.DataSource.DriverName, url)
6159
if err != nil {
6260
return err

pushing/bootstrap.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ import (
2222
)
2323

2424
func StartServer() {
25-
gbeConfig, err := conf.GetConfig()
26-
if err != nil {
27-
panic(err)
28-
}
25+
gbeConfig := conf.GetConfig()
2926

3027
sub := newSubscription()
3128

pushing/order_book.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,7 @@ var onceStore sync.Once
196196

197197
func sharedSnapshotStore() *redisSnapshotStore {
198198
onceStore.Do(func() {
199-
gbeConfig, err := conf.GetConfig()
200-
if err != nil {
201-
panic(err)
202-
}
199+
gbeConfig := conf.GetConfig()
203200

204201
redisClient := redis.NewClient(&redis.Options{
205202
Addr: gbeConfig.Redis.Addr,

pushing/redis_stream.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ import (
2626
)
2727

2828
type redisStream struct {
29-
sub *subscription
30-
mutex sync.Mutex
31-
gbeConfig *conf.GbeConfig
29+
sub *subscription
30+
mutex sync.Mutex
3231
}
3332

3433
func newRedisStream(sub *subscription) *redisStream {
@@ -39,18 +38,14 @@ func newRedisStream(sub *subscription) *redisStream {
3938
}
4039

4140
func (s *redisStream) Start() {
42-
gbeConf, err := conf.GetConfig()
43-
if err != nil {
44-
panic(err)
45-
}
46-
s.gbeConfig = gbeConf
41+
gbeConfig := conf.GetConfig()
4742

4843
redisClient := redis.NewClient(&redis.Options{
49-
Addr: s.gbeConfig.Redis.Addr,
50-
Password: s.gbeConfig.Redis.Password,
44+
Addr: gbeConfig.Redis.Addr,
45+
Password: gbeConfig.Redis.Password,
5146
DB: 0,
5247
})
53-
_, err = redisClient.Ping().Result()
48+
_, err := redisClient.Ping().Result()
5449
if err != nil {
5550
panic(err)
5651
}

rest/bootstrap.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ import (
2020
)
2121

2222
func StartServer() {
23-
gbeConfig, err := conf.GetConfig()
24-
if err != nil {
25-
panic(err)
26-
}
23+
gbeConfig := conf.GetConfig()
2724

2825
httpServer := NewHttpServer(gbeConfig.RestServer.Addr)
2926
go httpServer.Start()

0 commit comments

Comments
 (0)