-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbreaker.go
More file actions
131 lines (116 loc) · 3.09 KB
/
breaker.go
File metadata and controls
131 lines (116 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2023 TiKV Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cse
import (
"errors"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
"github.com/sony/gobreaker"
)
const (
open uint32 = iota
closed
)
var (
errUnavailable = errors.New("resource unavailable")
)
type asyncBreaker struct {
cb *gobreaker.CircuitBreaker
state uint32
done chan struct{}
once sync.Once
probeMinInterval time.Duration
probeMaxInterval time.Duration
}
type settings struct {
Name string
MaxRequests uint32
Interval time.Duration
Timeout time.Duration
ProbeMinInterval time.Duration
ProbeMaxInterval time.Duration
ReadyToTrip func(counts gobreaker.Counts) bool
IsSuccessful func(err error) bool
Probe func(string) error
}
func newAsyncBreaker(s settings) *asyncBreaker {
if s.ProbeMinInterval < 0 || s.ProbeMaxInterval < 0 || s.ProbeMinInterval > s.ProbeMaxInterval {
panic("invalid probe interval settings")
}
breaker := &asyncBreaker{
state: closed,
done: make(chan struct{}, 1),
probeMinInterval: s.ProbeMinInterval,
probeMaxInterval: s.ProbeMaxInterval,
}
cbs := gobreaker.Settings{
Name: s.Name,
MaxRequests: s.MaxRequests,
Interval: s.Interval,
Timeout: s.Timeout,
ReadyToTrip: s.ReadyToTrip,
IsSuccessful: s.IsSuccessful,
}
cbs.OnStateChange = func(_ string, from gobreaker.State, to gobreaker.State) {
if from == gobreaker.StateClosed && to == gobreaker.StateOpen {
breaker.openWith(s.Probe)
}
}
breaker.cb = gobreaker.NewCircuitBreaker(cbs)
return breaker
}
func (b *asyncBreaker) Close() {
b.once.Do(func() {
b.done <- struct{}{}
close(b.done)
})
}
func (b *asyncBreaker) openWith(probe func(string) error) bool {
success := atomic.CompareAndSwapUint32(&b.state, closed, open)
if success {
go b.probeLoop(probe)
}
return success
}
func (b *asyncBreaker) nextProbeInterval() time.Duration {
return b.probeMinInterval + time.Duration(rand.Int64N(int64(b.probeMaxInterval)-int64(b.probeMinInterval)))
}
func (b *asyncBreaker) probeLoop(probe func(string) error) {
ticker := time.NewTicker(b.nextProbeInterval())
defer ticker.Stop()
for {
select {
case <-ticker.C:
err := probe(b.cb.Name())
if err != nil {
ticker.Reset(b.nextProbeInterval())
continue
}
atomic.CompareAndSwapUint32(&b.state, open, closed)
return
case <-b.done:
return
}
}
}
func (b *asyncBreaker) Execute(f func() (any, error)) (any, error) {
return b.cb.Execute(func() (any, error) {
if atomic.LoadUint32(&b.state) == open {
return nil, errUnavailable
}
return f()
})
}