-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make backend CB individually configurable and separate cascade
Refactor backend instantiation such that circuit breaker backends are individually configurable. Reflect changes across the repo. Add the ability to check if a backend matches a request before attempting to call it. This avoids response slow-down caused by slow backends early in the chain Introduce a separate configuration to mark cascade lookup backends with explicit matcher in order to avoid routing all requests to them. Relates to: - #86
- Loading branch information
Showing
11 changed files
with
299 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/mercari/go-circuitbreaker" | ||
) | ||
|
||
var Matchers struct { | ||
Any HttpRequestMatcher | ||
AnyOf func(...HttpRequestMatcher) HttpRequestMatcher | ||
QueryParam func(key, value string) HttpRequestMatcher | ||
} | ||
|
||
type ( | ||
HttpRequestMatcher func(r *http.Request) bool | ||
Backend interface { | ||
URL() *url.URL | ||
CB() *circuitbreaker.CircuitBreaker | ||
Matches(r *http.Request) bool | ||
} | ||
SimpleBackend struct { | ||
url *url.URL | ||
cb *circuitbreaker.CircuitBreaker | ||
matcher HttpRequestMatcher | ||
} | ||
) | ||
|
||
func (b *SimpleBackend) URL() *url.URL { | ||
return b.url | ||
} | ||
|
||
func (b *SimpleBackend) CB() *circuitbreaker.CircuitBreaker { | ||
return b.cb | ||
} | ||
|
||
func init() { | ||
Matchers.Any = func(*http.Request) bool { return true } | ||
Matchers.AnyOf = func(ms ...HttpRequestMatcher) HttpRequestMatcher { | ||
return func(r *http.Request) bool { | ||
for _, m := range ms { | ||
if m(r) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
Matchers.QueryParam = func(key, value string) HttpRequestMatcher { | ||
return func(r *http.Request) bool { | ||
if r == nil { | ||
return false | ||
} | ||
values, ok := r.URL.Query()[key] | ||
if !ok { | ||
return false | ||
} | ||
for _, got := range values { | ||
if value == got { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
} | ||
} | ||
|
||
func NewBackend(u string, cb *circuitbreaker.CircuitBreaker, matcher HttpRequestMatcher) (Backend, error) { | ||
burl, err := url.Parse(u) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SimpleBackend{ | ||
url: burl, | ||
cb: cb, | ||
matcher: matcher, | ||
}, nil | ||
} | ||
|
||
func (b *SimpleBackend) Matches(r *http.Request) bool { | ||
return b.matcher(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.