-
Notifications
You must be signed in to change notification settings - Fork 46
/
port_registry.go
165 lines (133 loc) · 4.24 KB
/
port_registry.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package controllers
import (
"errors"
"fmt"
"github.com/go-logr/logr"
mpsv1alpha1 "github.com/playfab/thundernetes/operator/api/v1alpha1"
)
// PortRegistry implements a custom map for the port registry
type PortRegistry struct {
HostPorts map[int32]bool
Indexes []int32
NextFreePortIndex int32
Min int32 // Minimum Port
Max int32 // Maximum Port
portRequests chan struct{} // buffered channel to store port requests (request is the containerPort)
portResponses chan int32 // buffered channel to store port responses (system returns the HostPort)
}
// NewPortRegistry initializes the IndexedDictionary that holds the port registry.
func NewPortRegistry(gameServers mpsv1alpha1.GameServerList, min, max int32, setupLog logr.Logger) (*PortRegistry, error) {
pr := &PortRegistry{
HostPorts: make(map[int32]bool, max-min+1),
Indexes: make([]int32, max-min+1),
Min: min,
Max: max,
portRequests: make(chan struct{}, 100),
portResponses: make(chan int32, 100),
}
// gather ports for existing game servers
if len(gameServers.Items) > 0 {
for _, gs := range gameServers.Items {
if len(gs.Spec.PodSpec.Containers) == 0 {
setupLog.Info("GameServer with name %s has no containers in its Pod Template: %#v", gs.Name, gs)
continue
}
for _, container := range gs.Spec.PodSpec.Containers {
portsExposed := make([]int32, len(container.Ports))
portsExposedIndex := 0
for _, portInfo := range container.Ports {
if portInfo.HostPort == 0 {
setupLog.Info("HostPort for GameServer %s and ContainerPort %d is zero, ignoring", gs.Name, portInfo.ContainerPort)
continue
}
portsExposed[portsExposedIndex] = portInfo.HostPort
portsExposedIndex++
}
pr.assignRegisteredPorts(portsExposed)
}
}
}
pr.assignUnregisteredPorts()
go pr.portProducer()
return pr, nil
}
func (pr *PortRegistry) displayRegistry() {
fmt.Printf("-------------------------------------\n")
fmt.Printf("Ports: %v\n", pr.HostPorts)
fmt.Printf("Indexes: %v\n", pr.Indexes)
fmt.Printf("NextIndex: %d\n", pr.NextFreePortIndex)
fmt.Printf("-------------------------------------\n")
}
// GetNewPort returns and registers a new port for the designated game server. Locks a mutex
func (pr *PortRegistry) GetNewPort() (int32, error) {
pr.portRequests <- struct{}{}
port := <-pr.portResponses
if port == -1 {
return -1, errors.New("cannot register a new port. No available ports")
}
return port, nil
}
func (pr *PortRegistry) portProducer() {
for range pr.portRequests { //wait till a new request comes
initialIndex := pr.NextFreePortIndex
for {
if !pr.HostPorts[pr.Indexes[pr.NextFreePortIndex]] {
//we found a port
port := pr.Indexes[pr.NextFreePortIndex]
pr.HostPorts[port] = true
pr.increaseNextFreePortIndex()
//port is set
pr.portResponses <- port
break
}
pr.increaseNextFreePortIndex()
if initialIndex == pr.NextFreePortIndex {
//we did a full loop - no empty ports
pr.portResponses <- -1
break
}
}
}
}
// Stop stops port registry mechanism by closing requests and responses channels
func (pr *PortRegistry) Stop() {
close(pr.portRequests)
close(pr.portResponses)
}
// DeregisterServerPorts deregisters all host ports so they can be re-used by additional game servers
func (pr *PortRegistry) DeregisterServerPorts(ports []int32) {
for i := 0; i < len(ports); i++ {
pr.HostPorts[ports[i]] = false
}
}
func (pr *PortRegistry) assignRegisteredPorts(ports []int32) {
for i := 0; i < len(ports); i++ {
pr.HostPorts[ports[i]] = true
pr.Indexes[i] = ports[i]
pr.increaseNextFreePortIndex()
}
}
func (pr *PortRegistry) assignUnregisteredPorts() {
i := pr.NextFreePortIndex
for _, port := range pr.getPorts() {
if _, ok := pr.HostPorts[port]; !ok {
pr.HostPorts[port] = false
pr.Indexes[i] = port
i++
}
}
}
func (pr *PortRegistry) increaseNextFreePortIndex() {
pr.NextFreePortIndex++
//reset the index if needed
if pr.NextFreePortIndex == pr.Max-pr.Min+1 {
pr.NextFreePortIndex = 0
}
}
func (pr *PortRegistry) getPorts() []int32 {
ports := make([]int32, pr.Max-pr.Min+1)
for i := 0; i < len(ports); i++ {
ports[i] = int32(pr.Min) + int32(i)
}
return ports
}