-
Notifications
You must be signed in to change notification settings - Fork 2
/
cookie.go
58 lines (51 loc) · 1.46 KB
/
cookie.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
56
57
58
// Copyright © 2023 Brett Vickers.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nts
const (
cookieJarSize = 8
)
// A cookieJar is a ring-buffer that holds the 8 most recent NTS cookies
// supplied by the NTP server or NTS key-exchange server. Each NTS-based NTP
// query consumes a cookie from the cookie jar. The cookie jar is refilled
// after each successful NTS-based NTP query response.
type cookieJar struct {
cookies [cookieJarSize][]byte
count int
head int
tail int
}
// Add a cookie to the cookie jar. If the cookie jar is already full, the
// oldest cookie is replaced by the added cookie.
func (jar *cookieJar) Add(cookie []byte) {
jar.cookies[jar.head] = cookie
jar.head = (jar.head + 1) % cookieJarSize
if jar.count == cookieJarSize {
jar.tail = (jar.tail + 1) % cookieJarSize
} else {
jar.count++
}
}
// Clear the contents of the cookie jar.
func (jar *cookieJar) Clear() {
for i := range jar.cookies {
jar.cookies[i] = nil
}
jar.head, jar.tail, jar.count = 0, 0, 0
}
// Consume the oldest cookie in the cookie jar. If the jar is empty, return
// nil.
func (jar *cookieJar) Consume() []byte {
if jar.count == 0 {
return nil
}
cookie := jar.cookies[jar.tail]
jar.cookies[jar.tail] = nil
jar.tail = (jar.tail + 1) % cookieJarSize
jar.count--
return cookie
}
// Count returns the number of cookies in the cookie jar.
func (jar *cookieJar) Count() int {
return jar.count
}