From e886617f6372809b3fd4f0f5b139b660f18c648f Mon Sep 17 00:00:00 2001 From: favonia Date: Sun, 22 Aug 2021 22:59:17 -0500 Subject: [PATCH] feat(config): re-enable UPDATE_TIMEOUT; close #34 --- README.markdown | 3 +-- internal/config/config.go | 10 ++++++++-- internal/config/config_test.go | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.markdown b/README.markdown index 9bff5ca0..c622520a 100644 --- a/README.markdown +++ b/README.markdown @@ -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 | `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. diff --git a/internal/config/config.go b/internal/config/config.go index d1d68b8b..edd2fe54 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 } } @@ -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() @@ -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 @@ -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 } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c12546e5..4bb79d31 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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), @@ -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()) } @@ -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), @@ -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, @@ -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)