/
rpc_client.go
49 lines (43 loc) · 1.09 KB
/
rpc_client.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
package rpc_client
import (
// "errors"
"log"
"net/rpc"
"time"
)
type MikapodStorageService struct {
Client *rpc.Client
}
// Function will attempt to connect to the RPC server and if the attempt fails
// then wait 10 seconds and retry - if 100 attempts fail then a fatal panic
// will be triggered.
func retryMultipleAndBlockingDailHTTP(addr string) (*rpc.Client) {
i := 0
for {
client, err := rpc.DialHTTP("tcp", addr)
if err != nil {
i = i + 1
if i >= 100 {
log.Fatal("ERROR | MikapodStorageService | retryMultipleAndBlockingDailHTTP | Dialing TCP Error:", err)
}
time.Sleep(10 * time.Second)
} else {
return client
}
}
return nil
}
func New(addr string) *MikapodStorageService {
if addr == "" {
log.Fatal("LOG | MikapodStorageService | New | No address set.")
}
// The following code will dial the server or attempt to redial the server
// 100 times before it the client returns a fatal error.
client, err := rpc.DialHTTP("tcp", addr)
if err != nil {
client = retryMultipleAndBlockingDailHTTP(addr)
}
return &MikapodStorageService{
Client: client,
}
}