Simplify fully qualified SRV records for mapping (#92)

Co-authored-by: Geoff Bourne <itzgeoff@gmail.com>
This commit is contained in:
Merith 2022-06-26 07:37:21 -07:00 committed by GitHub
parent f441b148a1
commit 24381281e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -43,6 +43,8 @@ type Config struct {
MetricsBackend string `default:"discard" usage:"Backend to use for metrics exposure/publishing: discard,expvar,influxdb"`
UseProxyProtocol bool `default:"false" usage:"Send PROXY protocol to backend servers"`
MetricsBackendConfig MetricsBackendConfig
SimplifySRV bool `default:"false" usage:"Simplify fully qualified SRV records for mapping"`
}
var (
@ -129,6 +131,8 @@ func main() {
}
}
server.Routes.SimplifySRV(config.SimplifySRV)
err = metricsBuilder.Start(ctx)
if err != nil {
logrus.WithError(err).Fatal("Unable to start metrics reporter")

View File

@ -95,6 +95,7 @@ type IRoutes interface {
DeleteMapping(serverAddress string) bool
CreateMapping(serverAddress string, backend string, waker func(ctx context.Context) error)
SetDefaultRoute(backend string)
SimplifySRV(srvEnabled bool)
}
var Routes IRoutes = &routesImpl{}
@ -126,6 +127,7 @@ type routesImpl struct {
sync.RWMutex
mappings map[string]mapping
defaultRoute string
simplifySRV bool
}
func (r *routesImpl) SetDefaultRoute(backend string) {
@ -136,10 +138,31 @@ func (r *routesImpl) SetDefaultRoute(backend string) {
}).Info("Using default route")
}
func (r *routesImpl) SimplifySRV(srvEnabled bool) {
r.simplifySRV = srvEnabled
}
func (r *routesImpl) FindBackendForServerAddress(ctx context.Context, serverAddress string) (string, string, func(ctx context.Context) error) {
r.RLock()
defer r.RUnlock()
if r.simplifySRV {
serverAddress = strings.TrimSuffix(serverAddress, ".")
parts := strings.Split(serverAddress, ".")
tcpIndex := -1
for i, part := range parts {
if part == "_tcp" {
tcpIndex = i
break
}
}
if tcpIndex != -1 {
parts = parts[tcpIndex+1:]
}
serverAddress = strings.Join(parts, ".")
}
addressParts := strings.Split(serverAddress, "\x00")
address := strings.ToLower(addressParts[0])