-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathaudit_retry_config.go
More file actions
93 lines (77 loc) · 3.33 KB
/
Copy pathaudit_retry_config.go
File metadata and controls
93 lines (77 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package common
import (
"time"
"github.com/LFDT-Panurus/panurus/token/services/logging"
)
// AuditRetryConfigKey is the per-TMS configuration key (relative to the TMS
// configuration block) holding the audit-token retry/backoff settings consumed by
// AuditorCheck's RetrieveAuditTokens call.
const AuditRetryConfigKey = "auditor.auditTokensRetry"
// AuditRetryConfig holds the retry budget and backoff used by RetrieveAuditTokens
// to tolerate the pending-transaction read-timing race (issue #2105).
type AuditRetryConfig struct {
// NumRetries is the number of ListAuditTokens attempts made before giving up.
// A value <= 0 is clamped to a single attempt by RetrieveAuditTokens.
NumRetries int
// RetryDelay is the backoff slept between attempts (NumRetries-1 delays).
RetryDelay time.Duration
}
// DefaultAuditRetryConfig returns the audit-token retry configuration using the
// package default constants.
func DefaultAuditRetryConfig() AuditRetryConfig {
return AuditRetryConfig{
NumRetries: DefaultAuditTokensNumRetries,
RetryDelay: DefaultAuditTokensRetryDelay,
}
}
// AuditConfigProvider is the minimal configuration surface needed to load the
// audit-token retry configuration. It is satisfied by driver.Configuration (the
// per-TMS config) and is kept minimal so it is trivial to mock in tests.
type AuditConfigProvider interface {
// IsSet checks whether a configuration key is defined.
IsSet(key string) bool
// UnmarshalKey decodes the configuration value associated with a key into rawVal.
UnmarshalKey(key string, rawVal any) error
}
// auditRetryConfigRaw is the yaml-facing shape of AuditRetryConfigKey. RetryDelay
// is decoded as a duration string (e.g. "3s") so operators can express it in
// human-readable units.
type auditRetryConfigRaw struct {
NumRetries int `yaml:"numRetries"`
RetryDelay string `yaml:"retryDelay"`
}
// LoadAuditRetryConfig loads the audit-token retry configuration from the passed
// provider, overlaying any valid configured value onto DefaultAuditRetryConfig().
// A missing key, an unmarshal failure, or an individually invalid field leaves the
// corresponding default in place (a warning is logged for invalid values), so this
// function never fails: the audit gate always has a usable configuration.
func LoadAuditRetryConfig(cp AuditConfigProvider) AuditRetryConfig {
cfg := DefaultAuditRetryConfig()
if cp == nil || !cp.IsSet(AuditRetryConfigKey) {
return cfg
}
var raw auditRetryConfigRaw
if err := cp.UnmarshalKey(AuditRetryConfigKey, &raw); err != nil {
logging.MustGetLogger().Warnf("failed to unmarshal audit-token retry configuration [%s], using defaults: %v", AuditRetryConfigKey, err)
return cfg
}
// Apply the retry count if valid. A value <= 0 keeps the default; disabling
// retries entirely is intentionally not expressible here, matching the
// "at least one attempt" clamp in RetrieveAuditTokens.
if raw.NumRetries > 0 {
cfg.NumRetries = raw.NumRetries
}
// Apply the retry delay if valid.
if raw.RetryDelay != "" {
if duration, err := time.ParseDuration(raw.RetryDelay); err == nil && duration > 0 {
cfg.RetryDelay = duration
} else {
logging.MustGetLogger().Warnf("invalid retryDelay value [%s] for key [%s], using default", raw.RetryDelay, AuditRetryConfigKey)
}
}
return cfg
}