Skip to content

Commit 5d2ca65

Browse files
committed
fix: avoid o(2n) complexity in acl lookup
1 parent f841095 commit 5d2ca65

2 files changed

Lines changed: 14 additions & 18 deletions

File tree

‎internal/service/access_controls_service.go‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,21 @@ func NewAccessControlsService(
3030
}
3131

3232
func (service *AccessControlsService) lookupStaticACLs(domain string) *model.App {
33-
var appAcls *model.App
33+
var nameMatch *model.App
3434

35-
// first pass - try to find an exact match for the domain
35+
// First try to find a matching app by domain, then fallback to matching by app name (subdomain)
3636
for app, config := range service.config.Apps {
3737
if config.Config.Domain == domain {
3838
service.log.App.Debug().Str("name", app).Msg("Found matching container by domain")
39-
appAcls = &config
40-
break // If we find a match by domain, we can stop searching
39+
return &config
4140
}
42-
}
43-
44-
// second pass - if we didn't find a match by domain, try to find a match by app name (subdomain)
45-
for app, config := range service.config.Apps {
4641
if strings.SplitN(domain, ".", 2)[0] == app {
4742
service.log.App.Debug().Str("name", app).Msg("Found matching container by app name")
48-
appAcls = &config
49-
break // If we find a match by app name, we can stop searching
43+
nameMatch = &config
5044
}
5145
}
5246

53-
return appAcls
47+
return nameMatch
5448
}
5549

5650
func (service *AccessControlsService) GetAccessControls(domain string) (*model.App, error) {

‎internal/service/docker_service.go‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,23 @@ func (docker *DockerService) GetLabels(appDomain string) (*model.App, error) {
8585
return nil, err
8686
}
8787

88-
// fist pass - try to find an exact match for the domain
89-
for _, appLabels := range labels.Apps {
88+
var nameMatch *model.App
89+
90+
// First try to find a matching app by domain, then fallback to matching by app name (subdomain)
91+
for appName, appLabels := range labels.Apps {
9092
if appLabels.Config.Domain == appDomain {
9193
docker.log.App.Debug().Str("id", inspect.ID).Str("name", inspect.Name).Msg("Found matching container by domain")
9294
return &appLabels, nil
9395
}
94-
}
95-
96-
// second pass - if we didn't find a match by domain, try to find a match by app name (subdomain)
97-
for appName, appLabels := range labels.Apps {
9896
if strings.SplitN(appDomain, ".", 2)[0] == appName {
9997
docker.log.App.Debug().Str("id", inspect.ID).Str("name", inspect.Name).Msg("Found matching container by app name")
100-
return &appLabels, nil
98+
nameMatch = &appLabels
10199
}
102100
}
101+
102+
if nameMatch != nil {
103+
return nameMatch, nil
104+
}
103105
}
104106

105107
docker.log.App.Debug().Str("domain", appDomain).Msg("No matching container found for domain")

0 commit comments

Comments
 (0)