Skip to content

Commit fa2aa0b

Browse files
committed
fix: optimize code
1 parent 0ecfb3c commit fa2aa0b

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

route.go

+18-22
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,19 @@ func NewRoute(config config.Config, parameters map[string]any) (*Route, error) {
8383
func (r *Route) Fallback(handler httpcontract.HandlerFunc) {
8484
r.instance.Use(func(c fiber.Ctx) error {
8585
ctx := contextPool.Get().(*Context)
86+
defer func() {
87+
contextRequestPool.Put(ctx.request)
88+
contextResponsePool.Put(ctx.response)
89+
ctx.request = nil
90+
ctx.response = nil
91+
contextPool.Put(ctx)
92+
}()
8693

8794
ctx.instance = c
8895
if response := handler(ctx); response != nil {
8996
return response.Render()
9097
}
9198

92-
contextRequestPool.Put(ctx.request)
93-
contextResponsePool.Put(ctx.response)
94-
ctx.request = nil
95-
ctx.response = nil
96-
contextPool.Put(ctx)
97-
9899
return nil
99100
})
100101
}
@@ -139,17 +140,10 @@ func (r *Route) Run(host ...string) error {
139140
host = append(host, completeHost)
140141
}
141142

142-
network := fiber.NetworkTCP
143-
prefork := r.config.GetBool("http.drivers.fiber.prefork", false)
144-
// Fiber not support prefork on dual stack
145-
// https://docs.gofiber.io/api/fiber#config
146-
if prefork {
147-
network = fiber.NetworkTCP4
148-
}
149-
150143
r.outputRoutes()
151144
color.Green().Println(termlink.Link("[HTTP] Listening and serving HTTP on", "http://"+host[0]))
152145

146+
network, prefork := r.getNetworkConfig()
153147
return r.instance.Listen(host[0], fiber.ListenConfig{DisableStartupMessage: true, EnablePrefork: prefork, ListenerNetwork: network})
154148
}
155149

@@ -182,17 +176,10 @@ func (r *Route) RunTLSWithCert(host, certFile, keyFile string) error {
182176
return errors.New("certificate can't be empty")
183177
}
184178

185-
network := fiber.NetworkTCP
186-
prefork := r.config.GetBool("http.drivers.fiber.prefork", false)
187-
// Fiber not support prefork on dual stack
188-
// https://docs.gofiber.io/api/fiber#config
189-
if prefork {
190-
network = fiber.NetworkTCP4
191-
}
192-
193179
r.outputRoutes()
194180
color.Green().Println(termlink.Link("[HTTPS] Listening and serving HTTPS on", "https://"+host))
195181

182+
network, prefork := r.getNetworkConfig()
196183
return r.instance.Listen(host, fiber.ListenConfig{DisableStartupMessage: true, EnablePrefork: prefork, ListenerNetwork: network, CertFile: certFile, CertKeyFile: keyFile})
197184
}
198185

@@ -239,3 +226,12 @@ func (r *Route) setMiddlewares(middlewares []fiber.Handler) {
239226
r.instance.Use(middleware)
240227
}
241228
}
229+
230+
func (r *Route) getNetworkConfig() (string, bool) {
231+
network := fiber.NetworkTCP
232+
prefork := r.config.GetBool("http.drivers.fiber.prefork", false)
233+
if prefork {
234+
network = fiber.NetworkTCP4
235+
}
236+
return network, prefork
237+
}

0 commit comments

Comments
 (0)