mirror of
https://github.com/hanzoai/gateway.git
synced 2026-11-02 23:52:23 +00:00
Vet does not analyse module dependencies, so these sat unreported for as long as the engine lived in github.com/krakend/*. Folding it in put the code under our own `go vet ./...` and both surfaced immediately. Both are real, both fixes are behaviour-preserving, and neither is cosmetic: binder/context.go — Context.error passed a runtime string as the FORMAT argument of the printf-style LState.RaiseError. That string carries Lua script and host error text, so a '%' anywhere in a message is read as a verb: the error gets mangled and %!v(MISSING) is emitted from input an attacker can influence. Now passed as an argument. ratelimit/router/gin — a `return` after a switch whose every arm (including default) already returns. Unreachable; deleted. Also repoints two doc comments that still named upstream import paths, so the package docs describe where the code actually lives. Nothing else in internal/ deviates from upstream: 196 of 197 files remain byte-identical under the mechanical import rewrite, the lone exception being the bloomfilter subpackage rename from the previous commit. Re-run the check before any upstream re-sync so these four edits are carried forward deliberately rather than silently reverted. go vet ./... and go vet -tags legacy ./... -> both clean Co-authored-by: Hanzo Dev <[email protected]>
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
/*
|
|
Package gobreaker provides a circuit breaker proxy middleware using the sony/gobreaker lib.
|
|
|
|
Adding the middleware to your proxy stack
|
|
|
|
import (
|
|
"github.com/hanzoai/gateway/v2/internal/lura/proxy"
|
|
gobreaker "github.com/hanzoai/gateway/v2/internal/plugin/circuitbreaker/gobreaker/proxy"
|
|
)
|
|
|
|
...
|
|
|
|
var p proxy.Proxy
|
|
var backend *config.Backend
|
|
|
|
...
|
|
|
|
p = gobreaker.NewMiddleware(backend)(p)
|
|
|
|
...
|
|
*/
|
|
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hanzoai/gateway/v2/internal/lura/config"
|
|
"github.com/hanzoai/gateway/v2/internal/lura/logging"
|
|
"github.com/hanzoai/gateway/v2/internal/lura/proxy"
|
|
|
|
gcb "github.com/hanzoai/gateway/v2/internal/plugin/circuitbreaker/gobreaker"
|
|
)
|
|
|
|
// BackendFactory adds a cb middleware wrapping the internal factory
|
|
func BackendFactory(next proxy.BackendFactory, logger logging.Logger) proxy.BackendFactory {
|
|
return func(cfg *config.Backend) proxy.Proxy {
|
|
return NewMiddleware(cfg, logger)(next(cfg))
|
|
}
|
|
}
|
|
|
|
// NewMiddleware builds a middleware based on the extra config params or fallbacks to the next proxy
|
|
func NewMiddleware(remote *config.Backend, logger logging.Logger) proxy.Middleware {
|
|
data := gcb.ConfigGetter(remote.ExtraConfig).(gcb.Config)
|
|
if data == gcb.ZeroCfg {
|
|
return proxy.EmptyMiddleware
|
|
}
|
|
cb := gcb.NewCircuitBreaker(data, logger)
|
|
|
|
logger.Debug(fmt.Sprintf("[BACKEND: %s][CB] Creating the circuit breaker named '%s'", remote.URLPattern, data.Name))
|
|
|
|
return func(next ...proxy.Proxy) proxy.Proxy {
|
|
if len(next) > 1 {
|
|
panic(proxy.ErrTooManyProxies)
|
|
}
|
|
return func(ctx context.Context, request *proxy.Request) (*proxy.Response, error) {
|
|
result, err := cb.Execute(func() (interface{}, error) { return next[0](ctx, request) })
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.(*proxy.Response), err
|
|
}
|
|
}
|
|
}
|