diff --git a/common/types.go b/common/types.go deleted file mode 100644 index 8949021..0000000 --- a/common/types.go +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2021 yedf. All rights reserved. - * Use of this source code is governed by a BSD-style - * license that can be found in the LICENSE file. - */ - -package common - -import ( - "fmt" - "sync" - - "github.com/dtm-labs/dtm/dtmcli/logger" - "github.com/go-redis/redis/v8" -) - -var rdb *redis.Client -var once sync.Once - -func RedisGet() *redis.Client { - once.Do(func() { - logger.Debugf("connecting to redis: %v", Config.Store) - rdb = redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", Config.Store.Host, Config.Store.Port), - Username: Config.Store.User, - Password: Config.Store.Password, - }) - }) - return rdb -} diff --git a/common/types_test.go b/common/types_test.go index 7bb5500..72203d0 100644 --- a/common/types_test.go +++ b/common/types_test.go @@ -20,6 +20,7 @@ func TestGeneralDB(t *testing.T) { testDbAlone(t) } } + func testSql(t *testing.T) { db := DbGet(Config.Store.GetDBConf()) err := func() (rerr error) { diff --git a/dtmsvr/storage/redis/redis.go b/dtmsvr/storage/redis/redis.go index 60ec8fd..b32872b 100644 --- a/dtmsvr/storage/redis/redis.go +++ b/dtmsvr/storage/redis/redis.go @@ -3,6 +3,7 @@ package redis import ( "context" "fmt" + "sync" "time" "github.com/go-redis/redis/v8" @@ -14,10 +15,13 @@ import ( "github.com/dtm-labs/dtm/dtmsvr/storage" ) +// TODO: optimize this, it's very strange to use pointer to common.Config var config = &common.Config -var ctx context.Context = context.Background() +// TODO: optimize this, all function should have context as first parameter +var ctx = context.Background() +// RedisStore is the storage with redis, all transaction information will bachend with redis type RedisStore struct { } @@ -266,6 +270,19 @@ redis.call('SET', KEYS[1], ARGV[3], 'EX', ARGV[2]) dtmimp.E2P(err) } +var ( + rdb *redis.Client + once sync.Once +) + func redisGet() *redis.Client { - return common.RedisGet() + once.Do(func() { + logger.Debugf("connecting to redis: %v", config.Store) + rdb = redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%d", config.Store.Host, config.Store.Port), + Username: config.Store.User, + Password: config.Store.Password, + }) + }) + return rdb }