This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanager.go
173 lines (155 loc) · 3.54 KB
/
manager.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
166
167
168
169
170
171
172
173
package main
import (
"errors"
"log"
"slices"
"sync"
"sync/atomic"
"time"
)
var (
instancesLock sync.Mutex
instances []*instance
disallowInstanceCreation = &atomic.Bool{}
errCreationDisallowed = errors.New("instance creation disallowed")
errNoPortsDeclared = errors.New("no ports declared")
errNoFreePort = errors.New("no free ports")
)
func allocateNewInstance() (inst *instance, err error) {
instancesLock.Lock()
defer instancesLock.Unlock()
if disallowInstanceCreation.Load() {
return nil, errCreationDisallowed
}
ps, ok := cfg.GetString("ports")
if !ok {
return nil, errNoPortsDeclared
}
allowed := removeDuplicate(parseNumbersString(ps))
selected := 0
for _, p := range allowed {
used := false
for _, i := range instances {
if i.Settings.GamePort == p {
used = true
break
}
}
if !used {
selected = p
break
}
}
if selected == 0 {
return nil, errNoFreePort
}
inst = &instance{
Id: newInstanceID(),
Settings: instanceSettings{
GamePort: selected,
},
commands: make(chan instanceCommand, 32),
OnJoinDispatch: map[string]joinDispatch{},
wg: sync.WaitGroup{},
}
instances = append(instances, inst)
return
}
func insertInstance(inst *instance) bool {
if inst == nil {
log.Println("Inserting nil instance?!")
return false
}
instancesLock.Lock()
defer instancesLock.Unlock()
for i := range instances {
if instances[i].Id == inst.Id {
return false
}
if instances[i].Settings.GamePort == inst.Settings.GamePort {
return false
}
}
instances = append(instances, inst)
return true
}
func releaseInstance(inst *instance) {
if inst == nil {
log.Println("Releasing nil instance?!")
return
}
instancesLock.Lock()
instances = slices.DeleteFunc(instances, func(i *instance) bool {
return i.Id == inst.Id
})
instancesLock.Unlock()
}
func routineInstanceCleaner(closechan <-chan struct{}) {
for {
select {
case <-closechan:
return
case <-time.After(time.Second * time.Duration(cfg.GetDInt(30, "instanceCleanupTimer"))):
cleanInstances()
}
}
}
func cleanInstances() {
instancesLock.Lock()
instances = slices.DeleteFunc(instances, func(i *instance) bool {
if i.state.Load() == int64(instanceStateExited) {
log.Printf("Cleaned up instance %d", i.Id)
return true
}
return false
})
instancesLock.Unlock()
}
func stopAllRunners() {
instancesLock.Lock()
defer instancesLock.Unlock()
log.Printf("Ordering %d runners to quit processing", len(instances))
for _, v := range instances {
if cfg.GetDSBool(false, "shutdownHostsOnExit") {
v.commands <- instanceCommand{command: icShutdown}
} else {
v.commands <- instanceCommand{command: icRunnerStop}
}
}
log.Printf("Waiting for %d runners to quit", len(instances))
for _, v := range instances {
v.wg.Wait()
}
}
func isQueueInLobby(queueName string) int64 {
instancesLock.Lock()
defer instancesLock.Unlock()
return isQueueInLobbyNOLOCK(queueName)
}
func isQueueInLobbyNOLOCK(queueName string) int64 {
for i := range instances {
if instances[i].QueueName != queueName {
continue
}
if instances[i].state.Load() <= int64(instanceStateInLobby) {
return instances[i].Id
}
}
return 0
}
func isInstanceInLobby(instanceID int64) bool {
instancesLock.Lock()
defer instancesLock.Unlock()
return isInstanceInLobbyNOLOCK(instanceID)
}
func isInstanceInLobbyNOLOCK(instanceID int64) bool {
for i := range instances {
if instances[i].Id != instanceID {
continue
}
if instances[i].state.Load() <= int64(instanceStateInLobby) {
return true
}
}
return false
}