Skip to content

Commit 138040b

Browse files
committed
feat(options): panic when options are nil
Client creation should panic when options are nil.
1 parent 683f644 commit 138040b

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed

osscluster.go

+3
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,9 @@ type ClusterClient struct {
924924
// NewClusterClient returns a Redis Cluster client as described in
925925
// http://redis.io/topics/cluster-spec.
926926
func NewClusterClient(opt *ClusterOptions) *ClusterClient {
927+
if opt == nil {
928+
panic("redis: NewClusterClient nil options")
929+
}
927930
opt.init()
928931

929932
c := &ClusterClient{

redis.go

+3
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ type Client struct {
661661

662662
// NewClient returns a client to the Redis Server specified by Options.
663663
func NewClient(opt *Options) *Client {
664+
if opt == nil {
665+
panic("redis: NewClient nil options")
666+
}
664667
opt.init()
665668

666669
c := Client{

redis_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,54 @@ var _ = Describe("Dialer connection timeouts", func() {
727727
Expect(time.Since(start)).To(BeNumerically("<", 2*dialSimulatedDelay))
728728
})
729729
})
730+
var _ = Describe("Client creation", func() {
731+
Context("simple client with nil options", func() {
732+
It("panics", func() {
733+
Expect(func() {
734+
redis.NewClient(nil)
735+
}).To(Panic())
736+
})
737+
})
738+
Context("cluster client with nil options", func() {
739+
It("panics", func() {
740+
Expect(func() {
741+
redis.NewClusterClient(nil)
742+
}).To(Panic())
743+
})
744+
})
745+
Context("ring client with nil options", func() {
746+
It("panics", func() {
747+
Expect(func() {
748+
redis.NewRing(nil)
749+
}).To(Panic())
750+
})
751+
})
752+
Context("universal client with nil options", func() {
753+
It("panics", func() {
754+
Expect(func() {
755+
redis.NewUniversalClient(nil)
756+
}).To(Panic())
757+
})
758+
})
759+
Context("failover client with nil options", func() {
760+
It("panics", func() {
761+
Expect(func() {
762+
redis.NewFailoverClient(nil)
763+
}).To(Panic())
764+
})
765+
})
766+
Context("failover cluster client with nil options", func() {
767+
It("panics", func() {
768+
Expect(func() {
769+
redis.NewFailoverClusterClient(nil)
770+
}).To(Panic())
771+
})
772+
})
773+
Context("sentinel client with nil options", func() {
774+
It("panics", func() {
775+
Expect(func() {
776+
redis.NewSentinelClient(nil)
777+
}).To(Panic())
778+
})
779+
})
780+
})

ring.go

+3
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@ type Ring struct {
523523
}
524524

525525
func NewRing(opt *RingOptions) *Ring {
526+
if opt == nil {
527+
panic("redis: NewRing nil options")
528+
}
526529
opt.init()
527530

528531
hbCtx, hbCancel := context.WithCancel(context.Background())

sentinel.go

+11
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ func (opt *FailoverOptions) clusterOptions() *ClusterOptions {
224224
// for automatic failover. It's safe for concurrent use by multiple
225225
// goroutines.
226226
func NewFailoverClient(failoverOpt *FailoverOptions) *Client {
227+
if failoverOpt == nil {
228+
panic("redis: NewFailoverClient nil options")
229+
}
230+
227231
if failoverOpt.RouteByLatency {
228232
panic("to route commands by latency, use NewFailoverClusterClient")
229233
}
@@ -313,6 +317,9 @@ type SentinelClient struct {
313317
}
314318

315319
func NewSentinelClient(opt *Options) *SentinelClient {
320+
if opt == nil {
321+
panic("redis: NewSentinelClient nil options")
322+
}
316323
opt.init()
317324
c := &SentinelClient{
318325
baseClient: &baseClient{
@@ -828,6 +835,10 @@ func contains(slice []string, str string) bool {
828835
// NewFailoverClusterClient returns a client that supports routing read-only commands
829836
// to a replica node.
830837
func NewFailoverClusterClient(failoverOpt *FailoverOptions) *ClusterClient {
838+
if failoverOpt == nil {
839+
panic("redis: NewFailoverClusterClient nil options")
840+
}
841+
831842
sentinelAddrs := make([]string, len(failoverOpt.SentinelAddrs))
832843
copy(sentinelAddrs, failoverOpt.SentinelAddrs)
833844

universal.go

+4
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ var (
267267
// a ClusterClient is returned.
268268
// 4. Otherwise, a single-node Client is returned.
269269
func NewUniversalClient(opts *UniversalOptions) UniversalClient {
270+
if opts == nil {
271+
panic("redis: NewUniversalClient nil options")
272+
}
273+
270274
switch {
271275
case opts.MasterName != "" && (opts.RouteByLatency || opts.RouteRandomly || opts.IsClusterMode):
272276
return NewFailoverClusterClient(opts.Failover())

0 commit comments

Comments
 (0)