diff --git a/internal/dnsforward/http.go b/internal/dnsforward/http.go index d0fd07167da..b7428334b69 100644 --- a/internal/dnsforward/http.go +++ b/internal/dnsforward/http.go @@ -70,7 +70,7 @@ type jsonDNSConfig struct { DisableIPv6 *bool `json:"disable_ipv6"` // UpstreamMode defines the way DNS requests are constructed. - UpstreamMode *string `json:"upstream_mode"` + UpstreamMode *UpstreamMode `json:"upstream_mode"` // BlockedResponseTTL is the TTL for blocked responses. BlockedResponseTTL *uint32 `json:"blocked_response_ttl"` @@ -114,6 +114,22 @@ type jsonDNSConfig struct { DefaultLocalPTRUpstreams []string `json:"default_local_ptr_upstreams,omitempty"` } +// UpstreamMode is a enumeration of upstream modes. +type UpstreamMode string + +const ( + // UpstreamModeEmpty is the default value on frontend, it is used as + // UpstreamModeLoadBalance mode. + // + // Deprecated + // TODO(d.kolyshev): Use UpstreamModeLoadBalance instead. + UpstreamModeEmpty UpstreamMode = "" + + UpstreamModeLoadBalance UpstreamMode = "load_balance" + UpstreamModeParallel UpstreamMode = "parallel" + UpstreamModeFastestAddr UpstreamMode = "fastest_addr" +) + func (s *Server) getDNSConfig() (c *jsonDNSConfig) { protectionEnabled, protectionDisabledUntil := s.UpdatedProtectionStatus() @@ -145,14 +161,16 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) { usePrivateRDNS := s.conf.UsePrivateRDNS localPTRUpstreams := stringutil.CloneSliceOrEmpty(s.conf.LocalPTRResolvers) - var upstreamMode string - // TODO(d.kolyshev): Use 'load_balance' on frontend instead of nil as a - // default value. + var upstreamMode UpstreamMode switch s.conf.UpstreamMode { + case UpstreamModeTypeLoadBalance: + // TODO(d.kolyshev): Support UpstreamModeLoadBalance on frontend instead + // of UpstreamModeEmpty. + upstreamMode = UpstreamModeEmpty case UpstreamModeTypeParallel: - upstreamMode = "parallel" + upstreamMode = UpstreamModeParallel case UpstreamModeTypeFastestAddr: - upstreamMode = "fastest_addr" + upstreamMode = UpstreamModeFastestAddr } defPTRUps, err := s.defaultLocalPTRUpstreams() @@ -232,7 +250,11 @@ func (req *jsonDNSConfig) checkUpstreamMode() (err error) { } switch um := *req.UpstreamMode; um { - case "", "parallel", "fastest_addr", "load_balance": + case + UpstreamModeEmpty, + UpstreamModeLoadBalance, + UpstreamModeParallel, + UpstreamModeFastestAddr: return nil default: return fmt.Errorf("upstream_mode: incorrect value %q", um) @@ -466,13 +488,13 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) { // mustParseUpstreamMode returns an upstream mode parsed from string. Panics in // case of invalid value. -func mustParseUpstreamMode(mode string) (um string) { +func mustParseUpstreamMode(mode UpstreamMode) (um string) { switch mode { - case "", "load_balance": + case UpstreamModeEmpty, UpstreamModeLoadBalance: return UpstreamModeTypeLoadBalance - case "parallel": + case UpstreamModeParallel: return UpstreamModeTypeParallel - case "fastest_addr": + case UpstreamModeFastestAddr: return UpstreamModeTypeFastestAddr default: // Should never happen, since the value should be validated.