Skip to content

Commit 2dfdf45

Browse files
committed
WIP circuit breaker feature
1 parent ef811d8 commit 2dfdf45

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

caddy-modules/devxupstreamer/devxupstreamer.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"strconv"
66
"strings"
7+
"sync/atomic"
78

89
"github.com/caddyserver/caddy/v2"
910
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
@@ -16,9 +17,10 @@ import (
1617
)
1718

1819
type DevxUpstreamsModule struct {
19-
hub *sentry.Hub
20-
logger *zap.Logger
21-
listener *storelistener.Listener
20+
hub *sentry.Hub
21+
logger *zap.Logger
22+
listener *storelistener.Listener
23+
failureCount *atomic.Int64
2224
}
2325

2426
func init() {
@@ -29,15 +31,18 @@ func init() {
2931
// CaddyModule returns the Caddy module information.
3032
func (DevxUpstreamsModule) CaddyModule() caddy.ModuleInfo {
3133
return caddy.ModuleInfo{
32-
ID: "http.reverse_proxy.upstreams.devx",
33-
New: func() caddy.Module { return new(DevxUpstreamsModule) },
34+
ID: "http.reverse_proxy.upstreams.devx",
35+
New: func() caddy.Module {
36+
return new(DevxUpstreamsModule)
37+
},
3438
}
3539
}
3640

3741
func (d *DevxUpstreamsModule) Provision(ctx caddy.Context) error {
3842
// Get logger for the context of this caddy module instance
3943
d.logger = ctx.Logger(d)
4044
d.logger.Info("Provision called")
45+
d.failureCount = &atomic.Int64{}
4146
return nil
4247
}
4348

@@ -60,6 +65,11 @@ func (d *DevxUpstreamsModule) GetUpstreams(r *http.Request) ([]*reverseproxy.Ups
6065

6166
// If no upstream was selected log and return error
6267
if data.UpstreamHost == "" {
68+
if n := d.failureCount.Add(1); n > 10 {
69+
// FIXME: Shut down and restart listeners if we have several issues in a short period
70+
d.failureCount.Store(0)
71+
}
72+
6373
err := upstreamMissingError(data)
6474
d.logger.Warn(
6575
"Failed to get upstream",

0 commit comments

Comments
 (0)