fix(router): close httpLoader on shutdown, log parse errors, default nil logger
- Add missing httpLoader.Close() in localRouter.Close() to prevent resource leak - Log parse errors from file/redis/http loaders instead of silently discarding them - Add nil reader guards before calling parseRoutes in load() - Default nil logger to xlogger.Nop() to prevent nil panics - Use context.Background() instead of context.TODO() for root context - Use strings.ReplaceAll instead of deprecated strings.Replace with -1 - Add doc comments to all exported symbols - Add 27 unit tests + 1 benchmark
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// grpcPlugin implements router.Router via a gRPC plugin connection.
|
||||
type grpcPlugin struct {
|
||||
conn grpc.ClientConnInterface
|
||||
client proto.RouterClient
|
||||
@@ -44,6 +45,7 @@ func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) router.Route
|
||||
return p
|
||||
}
|
||||
|
||||
// GetRoute queries the gRPC plugin for the route to the given destination.
|
||||
func (p *grpcPlugin) GetRoute(ctx context.Context, dst string, opts ...router.Option) *router.Route {
|
||||
if p.client == nil {
|
||||
return nil
|
||||
@@ -67,6 +69,7 @@ func (p *grpcPlugin) GetRoute(ctx context.Context, dst string, opts ...router.Op
|
||||
return xrouter.ParseRoute(r.Dst, r.Gateway)
|
||||
}
|
||||
|
||||
// Close closes the underlying gRPC connection.
|
||||
func (p *grpcPlugin) Close() error {
|
||||
if p.conn == nil {
|
||||
return nil
|
||||
|
||||
@@ -11,17 +11,20 @@ import (
|
||||
xrouter "github.com/go-gost/x/router"
|
||||
)
|
||||
|
||||
// httpPluginGetRouteRequest is the request body for the HTTP route plugin.
|
||||
type httpPluginGetRouteRequest struct {
|
||||
Dst string `json:"dst"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
// httpPluginGetRouteResponse is the response body from the HTTP route plugin.
|
||||
type httpPluginGetRouteResponse struct {
|
||||
Net string `json:"net"`
|
||||
Dst string `json:"dst"`
|
||||
Gateway string `json:"gateway"`
|
||||
}
|
||||
|
||||
// httpPlugin implements router.Router via an HTTP plugin endpoint.
|
||||
type httpPlugin struct {
|
||||
url string
|
||||
client *http.Client
|
||||
@@ -47,6 +50,7 @@ func NewHTTPPlugin(name string, url string, opts ...plugin.Option) router.Router
|
||||
}
|
||||
}
|
||||
|
||||
// GetRoute queries the HTTP plugin for the route to the given destination.
|
||||
func (p *httpPlugin) GetRoute(ctx context.Context, dst string, opts ...router.Option) *router.Route {
|
||||
if p.client == nil {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user