Skip to content

Commit

Permalink
all middleware mark as optional by default
Browse files Browse the repository at this point in the history
  • Loading branch information
realityone committed Aug 17, 2023
1 parent c858635 commit eea532f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 60 deletions.
109 changes: 60 additions & 49 deletions api/gateway/config/v1/gateway.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/gateway/config/v1/gateway.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ message Endpoint {
message Middleware {
string name = 1;
google.protobuf.Any options = 2;
bool required = 3;
}

message Backend {
Expand Down
6 changes: 4 additions & 2 deletions api/gateway/middleware/circuitbreaker/v1/circuitbreaker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/gateway/middleware/cors/v1/cors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/gateway/middleware/logging/v1/logging.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/gateway/middleware/rewrite/v1/rewrite.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/gateway/middleware/tracing/v1/tracing.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/fatih/color v1.9.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-kratos/feature v0.0.0-20230724160043-79ea0633def6 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/go-kratos/aegis v0.1.1/go.mod h1:jYeSQ3Gesba478zEnujOiG5QdsyF3Xk/8owF
github.com/go-kratos/aegis v0.1.2/go.mod h1:jYeSQ3Gesba478zEnujOiG5QdsyF3Xk/8owFUeKcHxw=
github.com/go-kratos/aegis v0.2.1-0.20230616030432-99110a3f05f4 h1:LGUYBh6R1CGe1Vi5itL7fK2OZ+A4iim36Q0C5Y5ZjIs=
github.com/go-kratos/aegis v0.2.1-0.20230616030432-99110a3f05f4/go.mod h1:jqdJn8QOoobkmqfhO51kb/qUeHAQ8r0WpjPKl7cP3nQ=
github.com/go-kratos/feature v0.0.0-20230724160043-79ea0633def6 h1:Oz1K8FGRnFoX4KewhUFkdaD1+TcHGFlo6W1Br1n1gxw=
github.com/go-kratos/feature v0.0.0-20230724160043-79ea0633def6/go.mod h1:IDPI4pX5Twlm8gPSXATtVv4lItph3fVD2xGRMQm6les=
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20220318065833-e66a2905ab70 h1:/ksQcaaUTwEKNoH++wrs+u2M4lt34Ny75B+nUa1wLWk=
github.com/go-kratos/kratos/contrib/registry/consul/v2 v2.0.0-20220318065833-e66a2905ab70/go.mod h1:CFHMR6oi+wIEdqxjH4TwKvPlMWUfRY3SMke9++r/dB8=
github.com/go-kratos/kratos/v2 v2.2.1/go.mod h1:yebXu5KMayLjXZzMTY5HWIPRDwcBehHpiNF/Ot8A2pA=
Expand Down
7 changes: 7 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ func NewWithCloser(process Middleware, closer io.Closer) MiddlewareV2 {
closer: closer,
}
}

var EmptyMiddleware = emptyMiddleware{}

type emptyMiddleware struct{}

func (emptyMiddleware) Process(next http.RoundTripper) http.RoundTripper { return next }
func (emptyMiddleware) Close() error { return nil }
31 changes: 30 additions & 1 deletion middleware/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ import (
"strings"

configv1 "github.com/go-kratos/gateway/api/gateway/config/v1"
"github.com/go-kratos/kratos/v2/log"
"github.com/prometheus/client_golang/prometheus"
)

var LOG = log.NewHelper(log.With(log.GetLogger(), "source", "middleware"))
var globalRegistry = NewRegistry()
var _failedMiddlewareCreate = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "go",
Subsystem: "gateway",
Name: "failed_middleware_create",
Help: "The total number of failed middleware create",
}, []string{"name", "required"})

func init() {
prometheus.MustRegister(_failedMiddlewareCreate)
}

// ErrNotFound is middleware not found.
var ErrNotFound = errors.New("Middleware has not been registered")
Expand Down Expand Up @@ -42,7 +55,23 @@ func (p *middlewareRegistry) RegisterV2(name string, factory FactoryV2) {
// Create instantiates a middleware based on `cfg`.
func (p *middlewareRegistry) Create(cfg *configv1.Middleware) (MiddlewareV2, error) {
if method, ok := p.getMiddleware(createFullName(cfg.Name)); ok {
return method(cfg)
if cfg.Required {
// If the middleware is required, it must be created successfully.
instance, err := method(cfg)
if err != nil {
_failedMiddlewareCreate.WithLabelValues(cfg.Name, "true").Inc()
LOG.Errorw(log.DefaultMessageKey, "Failed to create required middleware", "reason", "create_required_middleware_failed", "name", cfg.Name, "error", err, "config", cfg)
return nil, err
}
return instance, nil
}
instance, err := method(cfg)
if err != nil {
_failedMiddlewareCreate.WithLabelValues(cfg.Name, "false").Inc()
LOG.Errorw(log.DefaultMessageKey, "Failed to create optional middleware", "reason", "create_optional_middleware_failed", "name", cfg.Name, "error", err, "config", cfg)
return EmptyMiddleware, nil
}
return instance, nil
}
return nil, ErrNotFound
}
Expand Down

0 comments on commit eea532f

Please sign in to comment.