Skip to content

Instantly share code, notes, and snippets.

// close circuit
func (c *Circuit) close() {
c.failuresCounter = 0
c.retriesCounter = 0
}
// increase failures counter circuit
func (c *Circuit) incrementFailures() {
c.failuresCounter++
c.retriesCounter++
func (c *Circuit) WithCircuit(call func() error, fallback func()) error {
if !c.isOpen() {
wait := make(chan error, 1)
// run function with timeout
go func() { wait <- call() }()
select {
case err := <-wait:
if err != nil {
func (c *Circuit) isOpen() bool {
if c.shouldRetry() {
return false
} else {
return c.failuresCounter > c.FailThreshold
}
}
func (c *Circuit) shouldRetry() bool {
if c.retriesCounter > c.RetryThreshold {
c.retriesCounter = 0
return true
}
return false
}
type Circuit struct {
TimeOut time.Duration // how long to wait for the execution
FailThreshold uint32 // how many fails until circuit is tripped
RetryThreshold uint32 // how many failed requests until try again
failuresCounter uint32
retriesCounter uint32
}
func (c *Circuit) WithCircuit(call func() error, fallback func()) error {
if !c.isOpen() {
wait := make(chan error, 1)
// run function with timeout
go func() { wait <- call() }()
select {
case err := <-wait:
if err != nil {
func fetchWeather(location string) string {
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
return "sunny"
}
LoadPlugin exec
<Plugin exec>
Exec "myuser:mygroup" "/path/to/another/binary" "arg0" "arg1"
</Plugin>
HOSTNAME="${COLLECTD_HOSTNAME:-localhost}"
INTERVAL="${COLLECTD_INTERVAL:-60}"
while sleep "$INTERVAL"; do
VALUE=do_magic()
echo "PUTVAL \"$HOSTNAME/exec-magic/gauge-magic_level\" interval=$INTERVAL N:$VALUE"
done
PUTVAL "$HOSTNAME/exec-magic/gauge-magic_level" interval=$INTERVAL N:$VALUE