-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
55 lines (43 loc) · 934 Bytes
/
rpc.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
54
55
package api
import (
"fmt"
"net/rpc"
"os"
"time"
)
const TIMEOUT = 2
func Update(args TaskState) error {
var reply Task
return Call("Master.UpdateTask", &args, &reply)
}
// send an RPC request to the master, wait for the response.
// usually returns true.
// returns false if something goes wrong.
func Call(rpcname string, args interface{}, reply interface{}) error {
c, err := rpc.DialHTTP("tcp", os.Getenv("MASTER_URL"))
if err != nil {
return err
}
defer c.Close()
_, timeout := handleRPC(func() bool {
err = c.Call(rpcname, args, reply)
return true
})
if timeout {
return fmt.Errorf("rpc call timed out")
}
return err
}
func handleRPC(rpcCall func() bool) (bool, bool) {
timeout := false
success := false
c := make(chan bool, 1)
go func() { c <- rpcCall() }()
select {
case s := <-c:
success = s
case <-time.After(TIMEOUT * time.Second):
timeout = true
}
return success, timeout
}