mirror of
https://github.com/yusing/godoxy.git
synced 2026-02-25 11:54:52 +01:00
refactor(entrypoint): streamline benchmark tests and enhance error handling
- Introduced `NewTestRoute` function to simplify route creation in benchmark tests. - Replaced direct route validation and starting with error handling using `require.NoError`. - Updated server retrieval to use `common.ProxyHTTPAddr` for consistency. - Improved logging for HTTP route addition errors in `AddRoute` method.
This commit is contained in:
@@ -126,10 +126,6 @@ func (ep *Entrypoint) GetServer(addr string) (http.Handler, bool) {
|
||||
return ep.servers.Load(addr)
|
||||
}
|
||||
|
||||
func (ep *Entrypoint) PrintServers() {
|
||||
log.Info().Msgf("servers: %v", xsync.ToPlainMap(ep.servers))
|
||||
}
|
||||
|
||||
func (ep *Entrypoint) SetFindRouteDomains(domains []string) {
|
||||
if len(domains) == 0 {
|
||||
ep.findRouteFunc = findRouteAnyDomain
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
. "github.com/yusing/godoxy/internal/entrypoint"
|
||||
entrypoint "github.com/yusing/godoxy/internal/entrypoint/types"
|
||||
"github.com/yusing/godoxy/internal/route"
|
||||
@@ -79,27 +81,20 @@ func BenchmarkEntrypointReal(b *testing.B) {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
r := &route.Route{
|
||||
r, err := route.NewTestRoute(b, task, &route.Route{
|
||||
Alias: "test",
|
||||
Scheme: routeTypes.SchemeHTTP,
|
||||
Host: host,
|
||||
Port: route.Port{Proxy: portInt},
|
||||
HealthCheck: types.HealthCheckConfig{Disable: true},
|
||||
}
|
||||
})
|
||||
|
||||
err = r.Validate()
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
err = r.Start(task)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
require.NoError(b, err)
|
||||
require.False(b, r.ShouldExclude())
|
||||
|
||||
var w noopResponseWriter
|
||||
|
||||
server, ok := ep.GetServer(r.ListenURL().Host)
|
||||
server, ok := ep.GetServer(common.ProxyHTTPAddr)
|
||||
if !ok {
|
||||
b.Fatal("server not found")
|
||||
}
|
||||
@@ -107,12 +102,12 @@ func BenchmarkEntrypointReal(b *testing.B) {
|
||||
b.ResetTimer()
|
||||
for b.Loop() {
|
||||
server.ServeHTTP(&w, &req)
|
||||
// if w.statusCode != http.StatusOK {
|
||||
// b.Fatalf("status code is not 200: %d", w.statusCode)
|
||||
// }
|
||||
// if string(w.written) != "1" {
|
||||
// b.Fatalf("written is not 1: %s", string(w.written))
|
||||
// }
|
||||
if w.statusCode != http.StatusOK {
|
||||
b.Fatalf("status code is not 200: %d", w.statusCode)
|
||||
}
|
||||
if string(w.written) != "1" {
|
||||
b.Fatalf("written is not 1: %s", string(w.written))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +122,7 @@ func BenchmarkEntrypoint(b *testing.B) {
|
||||
ep.SetFindRouteDomains([]string{})
|
||||
entrypoint.SetCtx(task, ep)
|
||||
|
||||
r := &route.Route{
|
||||
r, err := route.NewTestRoute(b, task, &route.Route{
|
||||
Alias: "test",
|
||||
Scheme: routeTypes.SchemeHTTP,
|
||||
Host: "localhost",
|
||||
@@ -137,27 +132,16 @@ func BenchmarkEntrypoint(b *testing.B) {
|
||||
HealthCheck: types.HealthCheckConfig{
|
||||
Disable: true,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
err := r.Validate()
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
require.NoError(b, err)
|
||||
require.False(b, r.ShouldExclude())
|
||||
|
||||
err = r.Start(task)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
rev, ok := ep.HTTPRoutes().Get("test")
|
||||
if !ok {
|
||||
b.Fatal("route not found")
|
||||
}
|
||||
rev.(types.ReverseProxyRoute).ReverseProxy().Transport = noopTransport{}
|
||||
r.(types.ReverseProxyRoute).ReverseProxy().Transport = noopTransport{}
|
||||
|
||||
var w noopResponseWriter
|
||||
|
||||
server, ok := ep.GetServer(r.ListenURL().Host)
|
||||
server, ok := ep.GetServer(common.ProxyHTTPAddr)
|
||||
if !ok {
|
||||
b.Fatal("server not found")
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
"github.com/yusing/godoxy/internal/types"
|
||||
)
|
||||
@@ -54,7 +55,13 @@ func (ep *Entrypoint) AddRoute(r types.Route) {
|
||||
}
|
||||
switch r := r.(type) {
|
||||
case types.HTTPRoute:
|
||||
ep.AddHTTPRoute(r)
|
||||
if err := ep.AddHTTPRoute(r); err != nil {
|
||||
log.Error().
|
||||
Err(err).
|
||||
Str("route", r.Key()).
|
||||
Str("listen_url", r.ListenURL().String()).
|
||||
Msg("failed to add HTTP route")
|
||||
}
|
||||
ep.shortLinkMatcher.AddRoute(r.Key())
|
||||
r.Task().OnCancel("remove_route", func() {
|
||||
ep.delHTTPRoute(r)
|
||||
@@ -90,9 +97,9 @@ func (ep *Entrypoint) AddHTTPRoute(route types.HTTPRoute) error {
|
||||
|
||||
func (ep *Entrypoint) addHTTPRoute(route types.HTTPRoute, addr string, proto HTTPProto) error {
|
||||
var err error
|
||||
srv, _ := ep.servers.LoadOrCompute(addr, func() (srv *httpServer, cancel bool) {
|
||||
srv = newHTTPServer(ep)
|
||||
err = srv.Listen(addr, proto)
|
||||
srv, _ := ep.servers.LoadOrCompute(addr, func() (newSrv *httpServer, cancel bool) {
|
||||
newSrv = newHTTPServer(ep)
|
||||
err = newSrv.Listen(addr, proto)
|
||||
cancel = err != nil
|
||||
return
|
||||
})
|
||||
|
||||
22
internal/route/test_route.go
Normal file
22
internal/route/test_route.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"github.com/yusing/godoxy/internal/types"
|
||||
"github.com/yusing/goutils/task"
|
||||
)
|
||||
|
||||
func NewTestRoute[T interface{ Helper() }](t T, task task.Parent, base *Route) (types.Route, error) {
|
||||
t.Helper()
|
||||
|
||||
err := base.Validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = base.Start(task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return base.impl, nil
|
||||
}
|
||||
Reference in New Issue
Block a user