Skip to content

feat(options): panic when options are nil #3363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions osscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,9 @@ type ClusterClient struct {
// NewClusterClient returns a Redis Cluster client as described in
// http://redis.io/topics/cluster-spec.
func NewClusterClient(opt *ClusterOptions) *ClusterClient {
if opt == nil {
panic("redis: NewClusterClient nil options")
}
opt.init()

c := &ClusterClient{
Expand Down
3 changes: 3 additions & 0 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,9 @@ type Client struct {

// NewClient returns a client to the Redis Server specified by Options.
func NewClient(opt *Options) *Client {
if opt == nil {
panic("redis: NewClient nil options")
}
opt.init()

c := Client{
Expand Down
51 changes: 51 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,3 +727,54 @@ var _ = Describe("Dialer connection timeouts", func() {
Expect(time.Since(start)).To(BeNumerically("<", 2*dialSimulatedDelay))
})
})
var _ = Describe("Client creation", func() {
Context("simple client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewClient(nil)
}).To(Panic())
})
})
Context("cluster client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewClusterClient(nil)
}).To(Panic())
})
})
Context("ring client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewRing(nil)
}).To(Panic())
})
})
Context("universal client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewUniversalClient(nil)
}).To(Panic())
})
})
Context("failover client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewFailoverClient(nil)
}).To(Panic())
})
})
Context("failover cluster client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewFailoverClusterClient(nil)
}).To(Panic())
})
})
Context("sentinel client with nil options", func() {
It("panics", func() {
Expect(func() {
redis.NewSentinelClient(nil)
}).To(Panic())
})
})
})
3 changes: 3 additions & 0 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ type Ring struct {
}

func NewRing(opt *RingOptions) *Ring {
if opt == nil {
panic("redis: NewRing nil options")
}
opt.init()

hbCtx, hbCancel := context.WithCancel(context.Background())
Expand Down
11 changes: 11 additions & 0 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func (opt *FailoverOptions) clusterOptions() *ClusterOptions {
// for automatic failover. It's safe for concurrent use by multiple
// goroutines.
func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
if failoverOpt == nil {
panic("redis: NewFailoverClient nil options")
}

if failoverOpt.RouteByLatency {
panic("to route commands by latency, use NewFailoverClusterClient")
}
Expand Down Expand Up @@ -313,6 +317,9 @@ type SentinelClient struct {
}

func NewSentinelClient(opt *Options) *SentinelClient {
if opt == nil {
panic("redis: NewSentinelClient nil options")
}
opt.init()
c := &SentinelClient{
baseClient: &baseClient{
Expand Down Expand Up @@ -828,6 +835,10 @@ func contains(slice []string, str string) bool {
// NewFailoverClusterClient returns a client that supports routing read-only commands
// to a replica node.
func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
if failoverOpt == nil {
panic("redis: NewFailoverClusterClient nil options")
}

sentinelAddrs := make([]string, len(failoverOpt.SentinelAddrs))
copy(sentinelAddrs, failoverOpt.SentinelAddrs)

Expand Down
4 changes: 4 additions & 0 deletions universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ var (
// a ClusterClient is returned.
// 4. Otherwise, a single-node Client is returned.
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
if opts == nil {
panic("redis: NewUniversalClient nil options")
}

switch {
case opts.MasterName != "" && (opts.RouteByLatency || opts.RouteRandomly || opts.IsClusterMode):
return NewFailoverClusterClient(opts.Failover())
Expand Down
Loading