Skip to content

Commit

Permalink
Always load backends in the order they are configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Oct 6, 2020
1 parent bef5433 commit f7ed6ad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/signaling/backend_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
for _, id := range strings.Split(backendIds, ",") {
id = strings.TrimSpace(id)
if id == "" {
continue
}

ids[id] = true
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)
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions src/signaling/backend_configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f7ed6ad

Please sign in to comment.