Always load backends in the order they are configured.

This commit is contained in:
Joachim Bauch 2020-10-06 15:58:05 +02:00
parent bef54339e3
commit f7ed6addb7
No known key found for this signature in database
GPG key ID: 77C1D22D53E15F02
2 changed files with 40 additions and 14 deletions

View file

@ -71,7 +71,7 @@ func NewBackendConfiguration(config *goconf.ConfigFile) (*BackendConfiguration,
compat: true,
}
} else if backendIds, _ := config.GetString("backend", "backends"); backendIds != "" {
for host, configuredBackends := range getConfiguredHosts(config) {
for host, configuredBackends := range getConfiguredHosts(backendIds, config) {
backends[host] = append(backends[host], configuredBackends...)
for _, be := range configuredBackends {
log.Printf("Backend %s added for %s", be.id, be.url)
@ -146,26 +146,28 @@ func (b *BackendConfiguration) UpsertHost(host string, backends []*Backend) {
b.backends[host] = append(b.backends[host], backends...)
}
func getConfiguredBackendIDs(config *goconf.ConfigFile) (ids map[string]bool) {
ids = make(map[string]bool)
func getConfiguredBackendIDs(backendIds string) (ids []string) {
seen := make(map[string]bool)
if backendIds, _ := config.GetString("backend", "backends"); backendIds != "" {
for _, id := range strings.Split(backendIds, ",") {
id = strings.TrimSpace(id)
if id == "" {
continue
}
ids[id] = true
for _, id := range strings.Split(backendIds, ",") {
id = strings.TrimSpace(id)
if id == "" {
continue
}
if seen[id] {
continue
}
ids = append(ids, id)
seen[id] = true
}
return ids
}
func getConfiguredHosts(config *goconf.ConfigFile) (hosts map[string][]*Backend) {
func getConfiguredHosts(backendIds string, config *goconf.ConfigFile) (hosts map[string][]*Backend) {
hosts = make(map[string][]*Backend)
for id := range getConfiguredBackendIDs(config) {
for _, id := range getConfiguredBackendIDs(backendIds) {
u, _ := config.GetString(id, "url")
if u == "" {
log.Printf("Backend %s is missing or incomplete, skipping", id)
@ -199,7 +201,7 @@ func getConfiguredHosts(config *goconf.ConfigFile) (hosts map[string][]*Backend)
func (b *BackendConfiguration) Reload(config *goconf.ConfigFile) {
if backendIds, _ := config.GetString("backend", "backends"); backendIds != "" {
configuredHosts := getConfiguredHosts(config)
configuredHosts := getConfiguredHosts(backendIds, config)
// remove backends that are no longer configured
for hostname := range b.backends {

View file

@ -165,6 +165,30 @@ func TestIsUrlAllowed_AllowAll(t *testing.T) {
testUrls(t, cfg, valid_urls, invalid_urls)
}
type ParseBackendIdsTestcase struct {
s string
ids []string
}
func TestParseBackendIds(t *testing.T) {
testcases := []ParseBackendIdsTestcase{
ParseBackendIdsTestcase{"", nil},
ParseBackendIdsTestcase{"backend1", []string{"backend1"}},
ParseBackendIdsTestcase{" backend1 ", []string{"backend1"}},
ParseBackendIdsTestcase{"backend1,", []string{"backend1"}},
ParseBackendIdsTestcase{"backend1,backend1", []string{"backend1"}},
ParseBackendIdsTestcase{"backend1, backend2", []string{"backend1", "backend2"}},
ParseBackendIdsTestcase{"backend1,backend2, backend1", []string{"backend1", "backend2"}},
}
for _, test := range testcases {
ids := getConfiguredBackendIDs(test.s)
if !reflect.DeepEqual(ids, test.ids) {
t.Errorf("List of ids differs, expected %+v, got %+v", test.ids, ids)
}
}
}
func TestBackendReloadChangeExistingURL(t *testing.T) {
original_config := goconf.NewConfigFile()
original_config.AddOption("backend", "backends", "backend1, backend2")