-
Notifications
You must be signed in to change notification settings - Fork 960
/
registry.go
56 lines (47 loc) · 1.26 KB
/
registry.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
package registry
import (
"time"
"github.com/dtm-labs/logger"
"github.com/dtm-labs/dtm/dtmsvr/config"
"github.com/dtm-labs/dtm/dtmsvr/storage"
"github.com/dtm-labs/dtm/dtmsvr/storage/boltdb"
"github.com/dtm-labs/dtm/dtmsvr/storage/redis"
"github.com/dtm-labs/dtm/dtmsvr/storage/sql"
)
var conf = &config.Config
// StorageFactory is factory to get storage instance.
type StorageFactory interface {
// GetStorage will return the Storage instance.
GetStorage() storage.Store
}
var sqlFac = &SingletonFactory{
creatorFunction: func() storage.Store {
return &sql.Store{}
},
}
var storeFactorys = map[string]StorageFactory{
"boltdb": &SingletonFactory{
creatorFunction: func() storage.Store {
return boltdb.NewStore(conf.Store.DataExpire, conf.RetryInterval)
},
},
"redis": &SingletonFactory{
creatorFunction: func() storage.Store {
return &redis.Store{}
},
},
"mysql": sqlFac,
"postgres": sqlFac,
"sqlserver": sqlFac,
}
// GetStore returns storage.Store
func GetStore() storage.Store {
return storeFactorys[conf.Store.Driver].GetStorage()
}
// WaitStoreUp wait for db to go up
func WaitStoreUp() {
for err := GetStore().Ping(); err != nil; err = GetStore().Ping() {
logger.Infof("wait store up: %v", err)
time.Sleep(3 * time.Second)
}
}