Skip to content

Commit 5845b74

Browse files
author
mirkobrombin
committed
chore: optimize virtual host routing with radix tree
Replaced linear domain lookup with a radix tree for a faster and more efficient routing system
1 parent 8e82b4e commit 5845b74

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/mirkobrombin/goup
33
go 1.22
44

55
require (
6-
github.com/mitchellh/mapstructure v1.5.0
6+
github.com/armon/go-radix v1.0.0
77
github.com/quic-go/quic-go v0.48.1
88
github.com/rivo/tview v0.0.0-20241103174730-c76f7879f592
99
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
2+
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
13
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
24
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
35
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -30,8 +32,6 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
3032
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
3133
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
3234
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
33-
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
34-
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
3535
github.com/onsi/ginkgo/v2 v2.9.5 h1:+6Hr4uxzP4XIUyAkg61dWBw8lb/gc4/X5luuxN/EC+Q=
3636
github.com/onsi/ginkgo/v2 v2.9.5/go.mod h1:tvAoo1QUJwNEU2ITftXTpR7R1RbCzoZUOs3RonqW57k=
3737
github.com/onsi/gomega v1.27.6 h1:ENqfyGeS5AX/rlXDd/ETokDz93u0YufY1Pgxuy/PvWE=

internal/server/server.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"sync"
99

10+
"github.com/armon/go-radix"
1011
"github.com/mirkobrombin/goup/internal/config"
1112
"github.com/mirkobrombin/goup/internal/logger"
1213
"github.com/mirkobrombin/goup/internal/plugin"
@@ -131,7 +132,7 @@ func startVirtualHostServer(port int, configs []config.SiteConfig, mwManager *mi
131132
identifier := fmt.Sprintf("port_%d", port)
132133
logger := loggers[identifier]
133134

134-
domainHandlers := make(map[string]http.Handler)
135+
radixTree := radix.New()
135136

136137
for _, conf := range configs {
137138
// We do not want to start a server if the root directory does not exist
@@ -148,19 +149,18 @@ func startVirtualHostServer(port int, configs []config.SiteConfig, mwManager *mi
148149
continue
149150
}
150151

151-
domainHandlers[conf.Domain] = handler
152+
radixTree.Insert(conf.Domain, handler)
152153
}
153154

154155
// Main handler that routes requests based on the Host header
155156
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
156157
host := r.Host
157-
158158
if colonIndex := strings.Index(host, ":"); colonIndex != -1 {
159159
host = host[:colonIndex]
160160
}
161161

162-
if handler, ok := domainHandlers[host]; ok {
163-
handler.ServeHTTP(w, r)
162+
if handler, found := radixTree.Get(host); found {
163+
handler.(http.Handler).ServeHTTP(w, r)
164164
} else {
165165
http.NotFound(w, r)
166166
}

0 commit comments

Comments
 (0)