-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
89 lines (76 loc) · 1.91 KB
/
request.go
File metadata and controls
89 lines (76 loc) · 1.91 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package qcache_inventory
/******************** Inventory Request
Sends a query for a key or an IP and provides a back-channel, so that the requesting partner can block on the request
until it arrives - honouring a timeout...
*/
import (
"strings"
"time"
"github.com/docker/docker/api/types"
"fmt"
)
type ContainerRequest struct {
IssuedAt time.Time
Source string
Timeout time.Duration
Name string
ID string
IP string
Back chan Response
}
func NewContainerRequest(src string, to time.Duration) ContainerRequest {
cr := ContainerRequest{
IssuedAt: time.Now(),
Source: src,
Timeout: to,
Back: make(chan Response, 5),
}
return cr
}
func NewIDContainerRequest(src, id string) ContainerRequest {
cr := NewContainerRequest(src, time.Second)
cr.ID = id
return cr
}
func NewNameContainerRequest(src, name string) ContainerRequest {
cr := NewContainerRequest(src, time.Second)
cr.Name = name
return cr
}
func NewIPContainerRequest(src, ip string) ContainerRequest {
cr := NewContainerRequest(src, time.Duration(2)*time.Second)
cr.IP = ip
return cr
}
func (this ContainerRequest) Equal(other Response) (err error) {
err = this.EqualCnt(other.Container)
if err != nil {
return
}
err = this.EqualIPS(other.Ips)
return
}
func (this ContainerRequest) EqualCnt(other *types.ContainerJSON) (err error) {
if this.ID == "" && this.Name == "" {
return
}
if this.ID == other.ID || this.Name == strings.Trim(other.Name, "/") {
return
}
return fmt.Errorf("this.ID:%s != %s:ID.other || this.Name:%s != %s:Name.other", this.ID, other.ID, this.Name, other.Name)
}
func (this ContainerRequest) EqualIPS(ips []string) (err error) {
if this.IP == "" {
return
}
for _, ip := range ips {
if this.IP == ip {
return
}
}
return fmt.Errorf("this.IP:%s not in %v", this.IP, ips)
}
func (cr *ContainerRequest) TimedOut() bool {
tDiff := time.Now().Sub(cr.IssuedAt)
return tDiff > cr.Timeout
}