From 2fdcd6aba5f37e567619a4a6c2aba12eccb9ffda Mon Sep 17 00:00:00 2001 From: Juan Calderon-Perez <835733+gaby@users.noreply.github.com> Date: Mon, 13 Oct 2025 21:33:01 -0400 Subject: [PATCH] Handle rueidis startup check failures --- rueidis/README.md | 2 ++ rueidis/config.go | 2 ++ rueidis/rueidis.go | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/rueidis/README.md b/rueidis/README.md index eef0d348..e6067d50 100644 --- a/rueidis/README.md +++ b/rueidis/README.md @@ -185,6 +185,8 @@ type Config struct { Reset bool // DisableStartupCheck skips the initial connection validation during New. + // When true and client creation fails, New returns a Storage whose + // operations surface the initialization error instead of panicking. // // Optional. Default is false DisableStartupCheck bool diff --git a/rueidis/config.go b/rueidis/config.go index c67ec7c2..c71937a9 100644 --- a/rueidis/config.go +++ b/rueidis/config.go @@ -96,6 +96,8 @@ type Config struct { Reset bool // DisableStartupCheck skips the initial connection validation during New. + // When true and the client cannot be created, New returns a Storage whose + // operations will report the initialization error instead of panicking. // // Optional. Default is false DisableStartupCheck bool diff --git a/rueidis/rueidis.go b/rueidis/rueidis.go index 0c4a72e8..3a26665c 100644 --- a/rueidis/rueidis.go +++ b/rueidis/rueidis.go @@ -11,7 +11,8 @@ var cacheTTL = time.Second // Storage interface that is implemented by storage providers type Storage struct { - db rueidis.Client + db rueidis.Client + initErr error } // New creates a new rueidis storage @@ -63,6 +64,10 @@ func New(config ...Config) *Storage { AlwaysPipelining: cfg.AlwaysPipelining, }) if err != nil { + if cfg.DisableStartupCheck { + return &Storage{initErr: err} + } + panic(err) } @@ -88,6 +93,9 @@ func New(config ...Config) *Storage { // GetWithContext gets value by key with context func (s *Storage) GetWithContext(ctx context.Context, key string) ([]byte, error) { + if s.db == nil { + return nil, s.initErr + } if len(key) <= 0 { return nil, nil } @@ -105,6 +113,9 @@ func (s *Storage) Get(key string) ([]byte, error) { // SetWithContext sets key with value with context func (s *Storage) SetWithContext(ctx context.Context, key string, val []byte, exp time.Duration) error { + if s.db == nil { + return s.initErr + } if len(key) <= 0 || len(val) <= 0 { return nil } @@ -122,6 +133,9 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error { // DeleteWithContext deletes key by key with context func (s *Storage) DeleteWithContext(ctx context.Context, key string) error { + if s.db == nil { + return s.initErr + } if len(key) <= 0 { return nil } @@ -135,6 +149,9 @@ func (s *Storage) Delete(key string) error { // ResetWithContext resets all keys with context func (s *Storage) ResetWithContext(ctx context.Context) error { + if s.db == nil { + return s.initErr + } return s.db.Do(ctx, s.db.B().Flushdb().Build()).Error() }