https://github.com/baalimago/watchdog
Simple watchdog implementation in golang
https://github.com/baalimago/watchdog
Last synced: 6 months ago
JSON representation
Simple watchdog implementation in golang
- Host: GitHub
- URL: https://github.com/baalimago/watchdog
- Owner: baalimago
- Created: 2022-05-31T19:11:25.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-25T17:45:18.000Z (8 months ago)
- Last Synced: 2025-02-25T18:39:12.392Z (8 months ago)
- Language: Go
- Size: 1000 Bytes
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Watchdog
Super simple watchdog implementation based on the existing timer functionality.
## Example
This is an example implementation where the objective is to gather buffer data as long as it comes in during a minimum update frequence, and then flush the buffer.
```
ctx, cancel := context.WithCancel(ctx.Background())
defer cancel()var buffer []interface{}
m := sync.Mutex{}
flushData := func() {
m.Lock()
defer m.Unlock()
flush(buffer)
}// Create a new watchdog and enter callback to call (such as a data flush operation)
w := watchdog.New(5 * time.Millisecond, flushData)go func() {
BREAK:
for {
select {
case <- ctx.Done():
break OUTER
case bufferableData := <- sourceChan:
// Pet the watchdog to keep it from flushing for another 5 milliseconds
w.Pet()
m.Lock()
buffer := append(buffer, bufferableData)
m.Unlock()
}
}
}()```
To avoid infinite Pets, use the `w.Stop()` functionality along with some separate timeout functionality (such as `context.WithTimeout`).