Refactor settings handling to support context-based reloading and add utility functions for context management
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
28
internal/settings/context.go
Normal file
28
internal/settings/context.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package settings
|
||||
|
||||
import "context"
|
||||
|
||||
type reloadedContextKey struct{}
|
||||
|
||||
// MarkReloadedInContext marks that a given Settings instance has been refreshed in this context flow.
|
||||
func MarkReloadedInContext(ctx context.Context, cfg *Settings) context.Context {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
if cfg == nil {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, reloadedContextKey{}, cfg)
|
||||
}
|
||||
|
||||
// IsReloadedInContext reports whether this context flow already refreshed the provided Settings.
|
||||
func IsReloadedInContext(ctx context.Context, cfg *Settings) bool {
|
||||
if ctx == nil || cfg == nil {
|
||||
return false
|
||||
}
|
||||
marked, ok := ctx.Value(reloadedContextKey{}).(*Settings)
|
||||
if !ok || marked == nil {
|
||||
return false
|
||||
}
|
||||
return marked == cfg
|
||||
}
|
||||
Reference in New Issue
Block a user