Skip to content

Commit

Permalink
Merge pull request #12 from rb3ckers/list-state
Browse files Browse the repository at this point in the history
Include details on current target state in GET response for target list
  • Loading branch information
rb3ckers authored Jan 13, 2020
2 parents 402f94c + 6cfaf5b commit d04c23d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
31 changes: 31 additions & 0 deletions datatypes/MirrorTargets.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type MirrorSettings struct {
RetryAfter time.Duration
}

type Target struct {
Name string
FailingSince time.Time
State string
}

type targetState struct {
sync.Mutex
firstFailure time.Time
Expand Down Expand Up @@ -66,6 +72,31 @@ func (mt *MirrorTargets) ForEach(f func(string, *gobreaker.CircuitBreaker)) {
}
}

func (mt *MirrorTargets) ListTargets() []Target {
targets := make([]Target, len(mt.targets)-1)

for url, target := range mt.targets {
var state string
switch target.circuitBreaker.State() {
case gobreaker.StateOpen:
state = "failing"
case gobreaker.StateHalfOpen:
state = "retrying"
case gobreaker.StateClosed:
state = "alive"
default:
state = "unknown"
}

targets = append(targets, Target{
Name: url,
FailingSince: target.firstFailure,
State: state,
})
}
return targets
}

func (ts *targetState) onBreakerChange(name string, from gobreaker.State, to gobreaker.State) {
if from == gobreaker.StateClosed && to == gobreaker.StateOpen {
ts.Lock()
Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ func mirrorTo(targetURL string, req *http.Request, body []byte, breaker *gobreak

func mirrorsHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodGet {
targets.ForEach(func(target string, breaker *gobreaker.CircuitBreaker) {
fmt.Fprintln(res, target)
})
for _, target := range targets.ListTargets() {
if target.State == "alive" {
fmt.Fprintf(res, "%s: %s\n", target.Name, target.State)
} else {
fmt.Fprintf(res, "%s: %s (since: %s)\n", target.Name, target.State, target.FailingSince.UTC().Format(time.RFC3339))
}
}
return
}

Expand Down

0 comments on commit d04c23d

Please sign in to comment.