Skip to content

Commit

Permalink
feat(config): re-enable UPDATE_TIMEOUT; close #34
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Aug 23, 2021
1 parent 045ca24 commit e886617
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
3 changes: 1 addition & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ In most cases, `CF_ACCOUNT_ID` is not needed.
| `TZ` | Recognized timezones, such as `UTC` | The timezone used for logging and parsing `UPDATE_CRON` | No | `UTC`
| `UPDATE_CRON` | Cron expressions; [documentation of cron](https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format). | The schedule to re-check IP addresses and update DNS records (if necessary) | No | `@every 5m` (every 5 minutes)
| `UPDATE_ON_START` | `1`, `t`, `T`, `TRUE`, `true`, `True`, `0`, `f`, `F`, `FALSE`, `false`, and `False` | Whether to check IP addresses on start regardless of `UPDATE_CRON` | No | `true`

<!-- | `UPDATE_TIMEOUT` | Positive time duration with a unit, such as `1h` or `10m`. See [time.ParseDuration](https://golang.org/pkg/time/#ParseDuration) | The timeout of each attempt to update DNS records, per domain, per record type | No | `1m` (1 minute) -->
| `UPDATE_TIMEOUT` | Positive time duration with a unit, such as `1h` or `10m`. See [time.ParseDuration](https://golang.org/pkg/time/#ParseDuration) | The timeout of each attempt to update DNS records, per domain, per record type | No | `30s` (30 seconds)

Note that the update schedule _does not_ take the time to update records into consideration. For example, if the schedule is “for every 5 minutes”, and if the updating itself takes 2 minutes, then the actual interval between adjacent updates is 3 minutes, not 5 minutes.
</details>
Expand Down
10 changes: 8 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Default() *Config {
CacheExpiration: time.Hour * 6, //nolint:gomnd
TTL: api.TTL(1),
Proxied: false,
UpdateTimeout: time.Hour,
UpdateTimeout: time.Second * 30,
DetectionTimeout: time.Second * 5, //nolint:gomnd
}
}
Expand Down Expand Up @@ -161,6 +161,10 @@ func ReadPolicyMap(ppfmt pp.PP, field *map[ipnet.Type]detector.Policy) bool {
}

func Print(ppfmt pp.PP, c *Config) {
if !ppfmt.IsEnabledFor(pp.Info) {
return
}

ppfmt.Infof(pp.EmojiEnvVars, "Current settings:")
ppfmt = ppfmt.IncIndent()

Expand Down Expand Up @@ -189,6 +193,7 @@ func Print(ppfmt pp.PP, c *Config) {

ppfmt.Infof(pp.EmojiConfig, "Timeouts")
inner.Infof(pp.EmojiBullet, "IP detection: %v", c.DetectionTimeout)
inner.Infof(pp.EmojiBullet, "Record updating: %v", c.UpdateTimeout)
}

func (c *Config) ReadEnv(ppfmt pp.PP) bool { //nolint:cyclop
Expand All @@ -206,7 +211,8 @@ func (c *Config) ReadEnv(ppfmt pp.PP) bool { //nolint:cyclop
!ReadNonnegDuration(ppfmt, "CACHE_EXPIRATION", &c.CacheExpiration) ||
!ReadNonnegInt(ppfmt, "TTL", (*int)(&c.TTL)) ||
!ReadBool(ppfmt, "PROXIED", &c.Proxied) ||
!ReadNonnegDuration(ppfmt, "DETECTION_TIMEOUT", &c.DetectionTimeout) {
!ReadNonnegDuration(ppfmt, "DETECTION_TIMEOUT", &c.DetectionTimeout) ||
!ReadNonnegDuration(ppfmt, "UPDATE_TIMEOUT", &c.UpdateTimeout) {
return false
}

Expand Down
18 changes: 17 additions & 1 deletion internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func TestPrintDefault(t *testing.T) {
mockPP := mocks.NewMockPP(mockCtrl)
innerMockPP := mocks.NewMockPP(mockCtrl)
gomock.InOrder(
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(true),
mockPP.EXPECT().Infof(pp.EmojiEnvVars, "Current settings:"),
mockPP.EXPECT().IncIndent().Return(mockPP),
mockPP.EXPECT().IncIndent().Return(innerMockPP),
Expand All @@ -319,6 +320,7 @@ func TestPrintDefault(t *testing.T) {
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Proxied: %t", false),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Timeouts"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IP detection: %v", time.Second*5),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Record updating: %v", time.Second*30),
)
config.Print(mockPP, config.Default())
}
Expand All @@ -332,6 +334,7 @@ func TestPrintEmpty(t *testing.T) {
mockPP := mocks.NewMockPP(mockCtrl)
innerMockPP := mocks.NewMockPP(mockCtrl)
gomock.InOrder(
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(true),
mockPP.EXPECT().Infof(pp.EmojiEnvVars, "Current settings:"),
mockPP.EXPECT().IncIndent().Return(mockPP),
mockPP.EXPECT().IncIndent().Return(innerMockPP),
Expand All @@ -349,12 +352,24 @@ func TestPrintEmpty(t *testing.T) {
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Proxied: %t", false),
mockPP.EXPECT().Infof(pp.EmojiConfig, "Timeouts"),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "IP detection: %v", time.Duration(0)),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Record updating: %v", time.Duration(0)),
)
config.Print(mockPP, &config.Config{})
}

func TestPrintHidden(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)

store(t, "TZ", "UTC")

mockPP := mocks.NewMockPP(mockCtrl)
mockPP.EXPECT().IsEnabledFor(pp.Info).Return(false)
config.Print(mockPP, &config.Config{})
}

//nolint:paralleltest // environment variables are global
func TestReadEnvOnlyToken(t *testing.T) {
func TestReadEnvWithOnlyToken(t *testing.T) {
mockCtrl := gomock.NewController(t)

unset(t,
Expand All @@ -381,6 +396,7 @@ func TestReadEnvOnlyToken(t *testing.T) {
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Use default %s=%d", "TTL", 0),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Use default %s=%t", "PROXIED", false),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Use default %s=%v", "DETECTION_TIMEOUT", time.Duration(0)),
innerMockPP.EXPECT().Infof(pp.EmojiBullet, "Use default %s=%v", "UPDATE_TIMEOUT", time.Duration(0)),
)
ok := cfg.ReadEnv(mockPP)
require.True(t, ok)
Expand Down

0 comments on commit e886617

Please sign in to comment.