-
Notifications
You must be signed in to change notification settings - Fork 3
/
ping.go
50 lines (45 loc) · 921 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
44
45
46
47
48
49
50
package main
import (
"bufio"
"fmt"
"io"
"os"
"syscall"
"time"
)
func main() {
parentpid, err := os.FindProcess(os.Getppid())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
// check last ping and reconnect if needed
ping := time.Now()
go func() {
for {
if time.Since(ping) > 355*time.Second {
fmt.Fprintf(os.Stderr, "ping not received from server for 255 sec, SIGUSR1 sent to %d\n", parentpid.Pid)
parentpid.Signal(syscall.SIGUSR1)
ping = time.Now()
}
time.Sleep(100 * time.Second)
}
}()
bio := bufio.NewReader(os.Stdin)
for {
line, err := bio.ReadString('\n')
if err != nil {
if err == io.EOF {
parentpid.Signal(syscall.SIGUSR1)
} else {
fmt.Fprintln(os.Stderr, err)
}
time.Sleep(2 * time.Second)
} else {
if len(line) > 4 && line[:4] == "PING" {
fmt.Printf(fmt.Sprintf("PONG%s", line[4:]))
ping = time.Now()
}
}
}
}