-
Notifications
You must be signed in to change notification settings - Fork 280
/
tinkerbell_hardware_queue.go
50 lines (43 loc) · 1.09 KB
/
tinkerbell_hardware_queue.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
package e2e
import (
"fmt"
"sync"
"time"
"github.com/aws/eks-anywhere/internal/pkg/api"
)
// Default timeout for Tink tests to poll for hardware.
const hwPollingTimeout = 120 * time.Minute
// hardwareCatalogue has a thread safe FIFO queue implementation to facilitate hardware reservation.
type hardwareCatalogue struct {
hws []*api.Hardware
mu sync.Mutex
}
func (hwQu *hardwareCatalogue) reserveHardware(count int) ([]*api.Hardware, error) {
now := time.Now()
after := now.Add(hwPollingTimeout)
for {
if now.After(after) {
return nil, fmt.Errorf("hardware polling request timed out")
}
hwQu.mu.Lock()
if count <= len(hwQu.hws) {
hardwareReserved := hwQu.hws[:count]
hwQu.hws = hwQu.hws[count:]
hwQu.mu.Unlock()
return hardwareReserved, nil
}
hwQu.mu.Unlock()
time.Sleep(1 * time.Minute)
}
}
func (hwQu *hardwareCatalogue) releaseHardware(hws []*api.Hardware) {
hwQu.mu.Lock()
hwQu.hws = append(hwQu.hws, hws...)
hwQu.mu.Unlock()
}
func newHardwareCatalogue(hws []*api.Hardware) *hardwareCatalogue {
return &hardwareCatalogue{
hws: hws,
mu: sync.Mutex{},
}
}