-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
feat: Add retry mechanism for storage initialization #3701
base: master
Are you sure you want to change the base?
Changes from 3 commits
a06ee77
abb1a0a
255cce4
5baca04
6dee3ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,8 +254,10 @@ type GRPC struct { | |
|
||
// Storage holds app's storage configuration. | ||
type Storage struct { | ||
Type string `json:"type"` | ||
Config StorageConfig `json:"config"` | ||
Type string `json:"type"` | ||
Config StorageConfig `json:"config"` | ||
RetryAttempts int `json:"retryAttempts"` | ||
RetryDelay string `json:"retryDelay"` | ||
} | ||
|
||
// StorageConfig is a configuration that can create a storage. | ||
|
@@ -297,8 +299,10 @@ var storages = map[string]func() StorageConfig{ | |
// dynamically determine the type of the storage config. | ||
func (s *Storage) UnmarshalJSON(b []byte) error { | ||
var store struct { | ||
Type string `json:"type"` | ||
Config json.RawMessage `json:"config"` | ||
Type string `json:"type"` | ||
Config json.RawMessage `json:"config"` | ||
RetryAttempts int `json:"retryAttempts"` | ||
RetryDelay string `json:"retryDelay"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to validate here that delay is in the go time format, e.g., |
||
} | ||
if err := json.Unmarshal(b, &store); err != nil { | ||
return fmt.Errorf("parse storage: %v", err) | ||
|
@@ -320,8 +324,10 @@ func (s *Storage) UnmarshalJSON(b []byte) error { | |
} | ||
} | ||
*s = Storage{ | ||
Type: store.Type, | ||
Config: storageConfig, | ||
Type: store.Type, | ||
Config: storageConfig, | ||
RetryAttempts: store.RetryAttempts, | ||
RetryDelay: store.RetryDelay, | ||
} | ||
return nil | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,7 +196,7 @@ func runServe(options serveOptions) error { | |
grpcOptions = append(grpcOptions, grpc.Creds(credentials.NewTLS(tlsConfig))) | ||
} | ||
|
||
s, err := c.Storage.Config.Open(logger) | ||
s, err := initializeStorageWithRetry(c.Storage, logger) | ||
if err != nil { | ||
return fmt.Errorf("failed to initialize storage: %v", err) | ||
} | ||
|
@@ -689,3 +689,37 @@ func loadTLSConfig(certFile, keyFile, caFile string, baseConfig *tls.Config) (*t | |
func recordBuildInfo() { | ||
buildInfo.WithLabelValues(version, runtime.Version(), fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)).Set(1) | ||
} | ||
|
||
// initializeStorageWithRetry opens a connection to the storage backend with a retry mechanism. | ||
func initializeStorageWithRetry(storageConfig Storage, logger *slog.Logger) (storage.Storage, error) { | ||
var s storage.Storage | ||
var err error | ||
|
||
retryAttempts := storageConfig.RetryAttempts | ||
if retryAttempts == 0 { | ||
retryAttempts = 5 // Default to 5 attempts | ||
} | ||
|
||
retryDelay, err := time.ParseDuration(storageConfig.RetryDelay) | ||
if err != nil { | ||
retryDelay = 5 * time.Second // Default to 5 seconds | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this code to the config parsing, we usually enforce config default there, not in the method. I expect it to be in |
||
|
||
for attempt := 1; attempt <= retryAttempts; attempt++ { | ||
s, err = storageConfig.Config.Open(logger) | ||
if err == nil { | ||
return s, nil | ||
} | ||
|
||
logger.Error("Failed to initialize storage", | ||
"attempt", fmt.Sprintf("%d/%d", attempt, retryAttempts), | ||
"error", err) | ||
|
||
if attempt < retryAttempts { | ||
logger.Info("Retrying storage initialization", | ||
"nextAttemptIn", retryDelay.String()) | ||
time.Sleep(retryDelay) | ||
} | ||
} | ||
return nil, fmt.Errorf("failed to initialize storage after %d attempts: %v", retryAttempts, err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.