Skip to content

Commit f63c955

Browse files
committed
simplified max tick computation by not relying on a 2nd max tick
1 parent 974b3e2 commit f63c955

File tree

2 files changed

+122
-16
lines changed

2 files changed

+122
-16
lines changed

node/container.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ func (c *Container) Update() error {
5353
log.Printf("Refreshing nodes...\n")
5454

5555
onlineNodes := fetchOnlineNodes(c.Addresses, c.Port, c.connectionTimeout)
56-
slices.SortFunc(onlineNodes, func(a, b *Node) int {
57-
return cmp.Compare(a.LastTick, b.LastTick)
58-
})
5956
maxTick := calculateMaxTick(onlineNodes, c.TickErrorThreshold)
6057

6158
reliableNodes, mostReliableNode := getReliableNodes(onlineNodes, maxTick, maxTick-c.ReliableTickRange)
@@ -114,24 +111,17 @@ func fetchOnlineNodes(addresses []string, port string, connectionTimeout time.Du
114111
}
115112

116113
func calculateMaxTick(nodes []*Node, threshold uint32) uint32 {
114+
slices.SortFunc(nodes, func(a, b *Node) int {
115+
return cmp.Compare(a.LastTick, b.LastTick)
116+
})
117117

118118
arrayLength := len(nodes)
119119

120120
if arrayLength == 0 {
121121
return 0
122122
}
123123

124-
if arrayLength < 2 {
125-
return nodes[0].LastTick
126-
}
127-
128-
maxTick := nodes[len(nodes)-1].LastTick
129-
maxTick2 := nodes[len(nodes)-2].LastTick
130-
131-
if maxTick2 != 0 && (maxTick-maxTick2) >= threshold {
132-
return maxTick2
133-
}
134-
return maxTick
124+
return nodes[arrayLength-1].LastTick
135125
}
136126

137127
func getReliableNodes(onlineNodes []*Node, maximum, minimum uint32) ([]*Node, *Node) {

node/container_test.go

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestMaxTick(t *testing.T) {
8484
LastUpdateSuccess: true,
8585
},
8686
},
87-
want: 2000,
87+
want: 3000,
8888
threshold: 50,
8989
},
9090
{
@@ -159,7 +159,7 @@ func TestMaxTick(t *testing.T) {
159159
LastUpdateSuccess: true,
160160
},
161161
},
162-
want: 2000,
162+
want: 2050,
163163
threshold: 50,
164164
},
165165
{
@@ -237,6 +237,122 @@ func TestMaxTick(t *testing.T) {
237237
want: 2049,
238238
threshold: 50,
239239
},
240+
{
241+
name: "TestMaxTick_4",
242+
nodes: []*Node{
243+
{
244+
Address: "",
245+
Peers: nil,
246+
LastTick: 1021,
247+
LastUpdate: 0,
248+
LastUpdateSuccess: true,
249+
},
250+
{
251+
Address: "",
252+
Peers: nil,
253+
LastTick: 1000,
254+
LastUpdate: 0,
255+
LastUpdateSuccess: true,
256+
},
257+
{
258+
Address: "",
259+
Peers: nil,
260+
LastTick: 1023,
261+
LastUpdate: 0,
262+
LastUpdateSuccess: true,
263+
},
264+
{
265+
Address: "",
266+
Peers: nil,
267+
LastTick: 1023,
268+
LastUpdate: 0,
269+
LastUpdateSuccess: true,
270+
},
271+
{
272+
Address: "",
273+
Peers: nil,
274+
LastTick: 2500,
275+
LastUpdate: 0,
276+
LastUpdateSuccess: true,
277+
},
278+
{
279+
Address: "",
280+
Peers: nil,
281+
LastTick: 5700,
282+
LastUpdate: 0,
283+
LastUpdateSuccess: true,
284+
}, {
285+
Address: "",
286+
Peers: nil,
287+
LastTick: 100,
288+
LastUpdate: 0,
289+
LastUpdateSuccess: true,
290+
},
291+
{
292+
Address: "",
293+
Peers: nil,
294+
LastTick: 1945,
295+
LastUpdate: 0,
296+
LastUpdateSuccess: true,
297+
},
298+
{
299+
Address: "",
300+
Peers: nil,
301+
LastTick: 2000,
302+
LastUpdate: 0,
303+
LastUpdateSuccess: true,
304+
}, {
305+
Address: "",
306+
Peers: nil,
307+
LastTick: 3000,
308+
LastUpdate: 0,
309+
LastUpdateSuccess: true,
310+
},
311+
},
312+
want: 5700,
313+
threshold: 50,
314+
},
315+
{
316+
name: "TestMaxTick_5",
317+
nodes: []*Node{
318+
{
319+
Address: "",
320+
Peers: nil,
321+
LastTick: 1000,
322+
LastUpdate: 0,
323+
LastUpdateSuccess: true,
324+
},
325+
},
326+
want: 1000,
327+
threshold: 50,
328+
},
329+
{
330+
name: "TestMaxTick_6",
331+
nodes: []*Node{
332+
{
333+
Address: "",
334+
Peers: nil,
335+
LastTick: 1500,
336+
LastUpdate: 0,
337+
LastUpdateSuccess: true,
338+
},
339+
{
340+
Address: "",
341+
Peers: nil,
342+
LastTick: 1000,
343+
LastUpdate: 0,
344+
LastUpdateSuccess: true,
345+
},
346+
},
347+
want: 1500,
348+
threshold: 50,
349+
},
350+
{
351+
name: "TestMaxTick_7",
352+
nodes: []*Node{},
353+
want: 0,
354+
threshold: 50,
355+
},
240356
}
241357

242358
for _, test := range testData {

0 commit comments

Comments
 (0)