Skip to content

Commit

Permalink
refactor: refactor comments in redis.go file
Browse files Browse the repository at this point in the history
- Remove `deadcode` and `depguard` linters from `.golangci.yml`
- Add `varcheck` linter to `.golangci.yml`
- Update comments in `redis.go` file:
  - Remove comment `// NewWorker for struc`
  - Add comment explaining `NewWorker` function and its responsibilities
  - Update comment explaining the initialization of Redis client in `NewWorker` function
  - Update comment explaining the condition for initializing Redis client in `NewWorker` function
  - Update comment explaining the initialization of Redis client with sentinel in `NewWorker` function
  - Update comment explaining the condition for initializing Redis client with sentinel in `NewWorker` function
  - Remove unnecessary code for initializing Redis client in `NewWorker` function

Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy committed Dec 5, 2023
1 parent 5b04ecc commit 71f87e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ linters:
disable-all: true
fast: false
enable:
- deadcode
- depguard
- dogsled
- dupl
- errcheck
Expand All @@ -30,7 +28,6 @@ linters:
- typecheck
- unconvert
- unused
- varcheck
- whitespace
- gofumpt

Expand Down
49 changes: 27 additions & 22 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,46 @@ type Worker struct {
opts options
}

// NewWorker for struc
// NewWorker creates a new Worker instance with the provided options.
// It initializes a Redis client based on the options and establishes a connection to the Redis server.
// The Worker is responsible for subscribing to a Redis channel and receiving messages from it.
// It returns the created Worker instance.
func NewWorker(opts ...Option) *Worker {
var err error
w := &Worker{
opts: newOptions(opts...),
stop: make(chan struct{}),
}

options := &redis.Options{
Addr: w.opts.addr,
Password: w.opts.password,
DB: w.opts.db,
}
w.rdb = redis.NewClient(options)

if w.opts.connectionString != "" {
options, err := redis.ParseURL(w.opts.connectionString)
if err != nil {
w.opts.logger.Fatal(err)
}
w.rdb = redis.NewClient(options)
} else if w.opts.addr != "" {
if w.opts.cluster {
w.rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(w.opts.addr, ","),
Password: w.opts.password,
})
} else if w.opts.sentinel {
w.rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: w.opts.masterName,
SentinelAddrs: strings.Split(w.opts.addr, ","),
Password: w.opts.password,
DB: w.opts.db,
})
} else {
options := &redis.Options{
Addr: w.opts.addr,
Password: w.opts.password,
DB: w.opts.db,
}
w.rdb = redis.NewClient(options)
}
}

if w.opts.cluster {
w.rdb = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(w.opts.addr, ","),
Password: w.opts.password,
})
}

if w.opts.sentinel {
w.rdb = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: w.opts.masterName,
SentinelAddrs: strings.Split(w.opts.addr, ","),
Password: w.opts.password,
DB: w.opts.db,
})
}

_, err = w.rdb.Ping(context.Background()).Result()
Expand Down

0 comments on commit 71f87e4

Please sign in to comment.