-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleton.go
53 lines (48 loc) · 1.21 KB
/
singleton.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
51
52
53
package main
import (
"io"
"log"
"net/http"
"os"
"strconv"
"time"
)
// Singleton makes sure there is only one "single" instance across the system.
// by binding to a tcp resource as specified by addr, eg. "127.0.0.1:51337".
func Singleton(addr string) {
go singletonServer(addr)
for {
// wait and confirm that server was started successfully
pid, err := getSingletonPID(addr)
if err == nil && pid == strconv.Itoa(os.Getpid()) {
return
}
time.Sleep(1 * time.Second)
}
}
func singletonServer(addr string) {
// Listen for incoming connections.
http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, strconv.Itoa(os.Getpid()))
})
err := http.ListenAndServe(addr, nil)
if err != nil {
pid, err := getSingletonPID(addr)
if err != nil {
log.Fatalln("agent already running, error on retriving pid", err)
}
log.Fatalln("agent already running on pid", pid)
}
}
func getSingletonPID(addr string) (string, error) {
resp, err := http.Get("http://" + addr + "/")
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}