-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.go
More file actions
108 lines (76 loc) · 2.09 KB
/
Copy pathQuery.go
File metadata and controls
108 lines (76 loc) · 2.09 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"sync"
"time"
)
type Query struct {
key string
nodeResponse map[string]int
nodeResponseMutex *sync.Mutex
chatterSize int
maxProcessFrequency int
statusCheckFrequencyInMilliSeconds int
printInfo bool
}
func NewQuery(key string, chatterSize, maxProcessFrequency, statusCheckFrequencyInMilliSeconds int, printInfo bool) *Query {
query := &Query{
key: key,
nodeResponse: make(map[string]int),
nodeResponseMutex: &sync.Mutex{},
chatterSize: chatterSize,
maxProcessFrequency: maxProcessFrequency,
statusCheckFrequencyInMilliSeconds: statusCheckFrequencyInMilliSeconds,
printInfo: printInfo,
}
query.statusDaemon()
return query
}
func (query *Query) UpdateNodeResponse(node *Node) bool {
query.nodeResponseMutex.Lock()
defer query.nodeResponseMutex.Unlock()
if _, ok := query.nodeResponse[node.name]; !ok {
query.nodeResponse[node.name] = node.Get(query.key)
return true
}
return false
}
func (query *Query) statusDaemon() {
fmt.Println(time.Now().String(), "|", "starting query daemon for key:", query.key)
go func() {
for {
if query.printInfo {
fmt.Println(time.Now().String(), "|", "query of key:", query.key, "response:", query.nodeResponse)
}
fmt.Println(time.Now().String(), "|", "aggregrate of key:", query.key, "val:", query.getAggregrate())
if query.hasConverged() {
return
}
time.Sleep(time.Millisecond * time.Duration(query.statusCheckFrequencyInMilliSeconds))
}
}()
}
func (query *Query) hasConverged() bool {
return false
}
func (query *Query) getAggregrate() int {
query.nodeResponseMutex.Lock()
defer query.nodeResponseMutex.Unlock()
val := 0
for _, res := range query.nodeResponse {
val += res
}
return val
}
/*
a
b
c
d
e
a -> c, d {a}
c -> b, e {a, c}
d -> a, b {a, c, d}
b -> a, d {a, c, d, b}
e -> b, c {a, c, d, b, e}
*/