/
timeout.go
47 lines (41 loc) · 1.02 KB
/
timeout.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
package types
import (
"encoding/json"
"fmt"
"time"
)
type ReplicaTimeout struct {
Replica ReplicaID `json:"replica"`
Type string `json:"type"`
Duration time.Duration `json:"duration"`
}
func (t *ReplicaTimeout) Key() string {
return fmt.Sprintf("%s_%s_%s", t.Replica, t.Type, t.Duration.String())
}
func (t *ReplicaTimeout) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"replica": string(t.Replica),
"type": t.Type,
"duration": t.Duration.String(),
})
}
func (t *ReplicaTimeout) Eq(other *ReplicaTimeout) bool {
return t.Replica == other.Replica && t.Type == other.Type && t.Duration == other.Duration
}
func TimeoutFromParams(replica ReplicaID, params map[string]string) (*ReplicaTimeout, bool) {
t := &ReplicaTimeout{
Replica: replica,
}
ttype, ok := params["type"]
if !ok {
return nil, false
}
t.Type = ttype
durS, ok := params["duration"]
dur, err := time.ParseDuration(durS)
if !ok || err != nil {
return nil, false
}
t.Duration = dur
return t, true
}