-
Notifications
You must be signed in to change notification settings - Fork 4
/
ping.go
43 lines (38 loc) · 979 Bytes
/
ping.go
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
package fluent
import (
"context"
"time"
)
// Ping is a helper method that allows you to call client.Ping in a periodic
// manner.
//
// By default a ping message will be sent every 5 minutes. You may change this
// using the WithPingInterval option.
//
// If you need to capture ping failures, pass it a channel using WithPingResultChan
func Ping(ctx context.Context, client Client, tag string, record interface{}, options ...Option) {
var interval = 5 * time.Minute
var replyCh chan error
//nolint:forcetypeassert
for _, option := range options {
switch option.Ident() {
case identPingInterval{}:
interval = option.Value().(time.Duration)
case identPingResultChan{}:
replyCh = option.Value().(chan error)
}
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
err := client.Ping(tag, record, options...)
if err != nil && replyCh != nil {
replyCh <- err
}
}
}
}