Skip to content

Commit

Permalink
return err
Browse files Browse the repository at this point in the history
  • Loading branch information
papa-hexuan committed Oct 26, 2022
1 parent 0458336 commit ecf7140
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
17 changes: 13 additions & 4 deletions pkg/client/redisgo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,26 @@ func (config *Config) MustSingleton() *Client {
// Build ..
func (config *Config) Build() (*Client, error) {
ins := new(Client)
var err error
if xdebug.IsDevelopmentMode() {
xdebug.PrettyJsonPrint("redisgo's config: "+config.name, config)
}
if config.Master.Addr != "" {
addr, user, pass := getUsernameAndPassword(config.Master.Addr)
ins.master = config.build(addr, user, pass)
ins.master, err = config.build(addr, user, pass)
if err != nil {
return ins, err
}
}
if len(config.Slaves.Addr) > 0 {
ins.slave = []*redis.Client{}
for _, slave := range config.Slaves.Addr {
addr, user, pass := getUsernameAndPassword(slave)
ins.slave = append(ins.slave, config.build(addr, user, pass))
cli, err := config.build(addr, user, pass)
if err != nil {
return ins, err
}
ins.slave = append(ins.slave, cli)
}
}

Expand All @@ -84,7 +92,7 @@ func (config *Config) Build() (*Client, error) {
return ins, nil
}

func (config *Config) build(addr, user, pass string) *redis.Client {
func (config *Config) build(addr, user, pass string) (*redis.Client, error) {

stubClient := redis.NewClient(&redis.Options{
Addr: addr,
Expand Down Expand Up @@ -118,10 +126,11 @@ func (config *Config) build(addr, user, pass string) *redis.Client {
config.logger.Panic("redisgo stub client start err: " + err.Error())
}
config.logger.Error("redisgo stub client start err", xlog.FieldErr(err))
return nil, err
}

instances.Store(config.name, &storeRedis{
ClientStub: stubClient,
})
return stubClient
return stubClient, nil
}
6 changes: 3 additions & 3 deletions pkg/client/redisgo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func Test_Stub(t *testing.T) {
assert.Nil(t, client)
}
}()
client, _ = config.Build()
client = config.MustSingleton()
assert.Nil(t, client) // 不会执行到这里
})
t.Run("should not panic when dial err", func(t *testing.T) {
config.Master.Addr = "1.1.1.1"
config.OnDialError = "error"
client, err := config.Build()
assert.Nil(t, err)
assert.NotNil(t, client.master)
assert.NotNil(t, err)
assert.Nil(t, client.master)

})
t.Run("normal start", func(t *testing.T) {
Expand Down

0 comments on commit ecf7140

Please sign in to comment.