|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package finality |
| 8 | + |
| 9 | +import "time" |
| 10 | + |
| 11 | +const ( |
| 12 | + // PollInterval is the configuration key for how often the shared poller sweeps the pending set |
| 13 | + PollInterval = "token.finality.poller.interval" |
| 14 | + // PollBatchSize is the configuration key for how many txIDs go into one committer status query |
| 15 | + PollBatchSize = "token.finality.poller.batchSize" |
| 16 | + // PendingTTL is the configuration key for how long a tx stays pending before its slot is |
| 17 | + // reclaimed; it should exceed the longest caller finality timeout |
| 18 | + PendingTTL = "token.finality.poller.pendingTTL" |
| 19 | + |
| 20 | + // DefaultPollInterval is the default sweep interval |
| 21 | + DefaultPollInterval = 1 * time.Second |
| 22 | + // DefaultPollBatchSize is the default status query batch size |
| 23 | + DefaultPollBatchSize = 2000 |
| 24 | + // DefaultPendingTTL is the default pending slot TTL |
| 25 | + DefaultPendingTTL = 10 * time.Minute |
| 26 | +) |
| 27 | + |
| 28 | +// ConfigGetter models the configuration getter for the finality poller |
| 29 | +type ConfigGetter interface { |
| 30 | + // PollInterval returns how often the shared poller sweeps the pending set |
| 31 | + PollInterval() time.Duration |
| 32 | + // PollBatchSize returns how many txIDs go into one committer status query |
| 33 | + PollBatchSize() int |
| 34 | + // PendingTTL returns how long a tx stays pending before its slot is reclaimed |
| 35 | + PendingTTL() time.Duration |
| 36 | +} |
| 37 | + |
| 38 | +// Configuration models the configuration for the finality poller. |
| 39 | +// |
| 40 | +//go:generate counterfeiter -o mock/configuration.go -fake-name Configuration . Configuration |
| 41 | +type Configuration interface { |
| 42 | + // GetDuration returns the duration for the given key. |
| 43 | + GetDuration(key string) time.Duration |
| 44 | + // GetInt returns the int for the given key. |
| 45 | + GetInt(key string) int |
| 46 | +} |
| 47 | + |
| 48 | +// NewConfig creates a new ConfigGetter that uses the provided Configuration |
| 49 | +// interface to retrieve finality poller settings. |
| 50 | +func NewConfig(configuration Configuration) *serviceConfig { |
| 51 | + return &serviceConfig{configuration: configuration} |
| 52 | +} |
| 53 | + |
| 54 | +type serviceConfig struct { |
| 55 | + configuration Configuration |
| 56 | +} |
| 57 | + |
| 58 | +// PollInterval returns the sweep interval from the configuration. |
| 59 | +// If the configured value is not greater than 0, it returns DefaultPollInterval. |
| 60 | +func (c *serviceConfig) PollInterval() time.Duration { |
| 61 | + if v := c.configuration.GetDuration(PollInterval); v > 0 { |
| 62 | + return v |
| 63 | + } |
| 64 | + |
| 65 | + return DefaultPollInterval |
| 66 | +} |
| 67 | + |
| 68 | +// PollBatchSize returns the status query batch size from the configuration. |
| 69 | +// If the configured value is not greater than 0, it returns DefaultPollBatchSize. |
| 70 | +func (c *serviceConfig) PollBatchSize() int { |
| 71 | + if v := c.configuration.GetInt(PollBatchSize); v > 0 { |
| 72 | + return v |
| 73 | + } |
| 74 | + |
| 75 | + return DefaultPollBatchSize |
| 76 | +} |
| 77 | + |
| 78 | +// PendingTTL returns the pending slot TTL from the configuration. |
| 79 | +// If the configured value is not greater than 0, it returns DefaultPendingTTL. |
| 80 | +func (c *serviceConfig) PendingTTL() time.Duration { |
| 81 | + if v := c.configuration.GetDuration(PendingTTL); v > 0 { |
| 82 | + return v |
| 83 | + } |
| 84 | + |
| 85 | + return DefaultPendingTTL |
| 86 | +} |
0 commit comments