Skip to content

Commit

Permalink
all middleware mark as optional by default (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
realityone authored Aug 17, 2023
1 parent c858635 commit 4787917
Show file tree
Hide file tree
Showing 9 changed files with 110 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.

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 4787917

Please sign in to comment.