This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
/
port_grabber.go
89 lines (73 loc) · 2.29 KB
/
port_grabber.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
/*
// Copyright (c) 2016 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
package main
import (
"sync"
"github.com/golang/glog"
)
const (
portGrabberStart = 5900
portGrabberMax = 6900
)
/*
Just because a port is in the free map doesn't mean it's free. It could be
used by some other qemu process or otherwise that is not managed by launcher.
In addition, we have the tricky case of restarting launcher after a crash
where there are already running instances. We could ask those instances for
their spice port and them remove them from the map (and perhaps we will in
the future), but there will still be a race condition, if a new start command
comes in while we query the domain socket of running instances.
To summarize, we need to try to detect the fact that qemu has failed due to
an in-use port and restart it with a new port. We could try to detect whether
a port was in use or not, a la libvirt, but there'd still be a race condition.
*/
type portGrabber struct {
sync.Mutex
free map[int]struct{}
}
var uiPortGrabber = portGrabber{}
func init() {
uiPortGrabber.free = make(map[int]struct{})
for i := portGrabberStart; i < portGrabberMax; i++ {
uiPortGrabber.free[i] = struct{}{}
}
}
func (pg *portGrabber) grabPort() int {
port := 0
pg.Lock()
glog.Infof("Ports available %d", len(pg.free))
for key := range pg.free {
port = key
break
}
if port != 0 {
delete(pg.free, port)
glog.Infof("Grabbing port: %d", port)
}
pg.Unlock()
return port
}
func (pg *portGrabber) releasePort(port int) {
glog.Infof("Releasing port: %d", port)
if port < portGrabberStart || port >= portGrabberMax {
glog.Warningf("Unable to release invalid port number %d", port)
return
}
pg.Lock()
pg.free[port] = struct{}{}
glog.Infof("Ports available %d", len(pg.free))
pg.Unlock()
}