This file contains hidden or 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
// close circuit | |
func (c *Circuit) close() { | |
c.failuresCounter = 0 | |
c.retriesCounter = 0 | |
} | |
// increase failures counter circuit | |
func (c *Circuit) incrementFailures() { | |
c.failuresCounter++ | |
c.retriesCounter++ |
This file contains hidden or 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
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 { |
This file contains hidden or 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
func (c *Circuit) isOpen() bool { | |
if c.shouldRetry() { | |
return false | |
} else { | |
return c.failuresCounter > c.FailThreshold | |
} | |
} |
This file contains hidden or 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
func (c *Circuit) shouldRetry() bool { | |
if c.retriesCounter > c.RetryThreshold { | |
c.retriesCounter = 0 | |
return true | |
} | |
return false | |
} |
This file contains hidden or 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
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 | |
} |
This file contains hidden or 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
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 { |
This file contains hidden or 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
func fetchWeather(location string) string { | |
time.Sleep(time.Duration(rand.Intn(5)) * time.Second) | |
return "sunny" | |
} |
This file contains hidden or 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
LoadPlugin exec | |
<Plugin exec> | |
Exec "myuser:mygroup" "/path/to/another/binary" "arg0" "arg1" | |
</Plugin> |
This file contains hidden or 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
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 |
This file contains hidden or 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
PUTVAL "$HOSTNAME/exec-magic/gauge-magic_level" interval=$INTERVAL N:$VALUE |