Skip to content

Commit e989d56

Browse files
committed
change(service-account-server): use a random free port
1 parent 2c5a43e commit e989d56

File tree

5 files changed

+55
-12
lines changed

5 files changed

+55
-12
lines changed

cmd/sync.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func performSync(s *syncer.Syncer) error {
126126
if strings.Contains(s.GlobalConfig.Rclone.Path, "gclone") {
127127
// start web-server
128128
s.Ws.Run()
129+
defer s.Ws.Stop()
129130

130131
gcloneParams = append(gcloneParams,
131132
"--drive-service-account-url",

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/onsi/ginkgo v1.12.0 // indirect
2020
github.com/onsi/gomega v1.9.0 // indirect
2121
github.com/pelletier/go-toml v1.7.0 // indirect
22+
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
2223
github.com/pkg/errors v0.9.1
2324
github.com/rhysd/go-github-selfupdate v1.2.2
2425
github.com/sirupsen/logrus v1.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
225225
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
226226
github.com/pelletier/go-toml v1.7.0 h1:7utD74fnzVc/cpcyy8sjrlFr5vYpypUixARcHIMIGuI=
227227
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=
228+
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 h1:JhzVVoYvbOACxoUmOs6V/G4D5nPVUW73rKvXxP4XUJc=
229+
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
228230
github.com/philhofer/fwd v1.0.0 h1:UbZqGr5Y38ApvM/V/jEljVxwocdweyH+vmYvRPBnbqQ=
229231
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
230232
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

syncer/syncer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func New(config *config.Configuration, syncerConfig *config.SyncerConfig, syncer
4040
Config: syncerConfig,
4141
Name: syncerName,
4242
RemoteServiceAccountFiles: sam,
43-
Ws: newWebServer("127.0.0.1", 3263, l, sam),
43+
Ws: newWebServer("127.0.0.1", l, syncerName, sam),
4444
}
4545

4646
return syncer, nil

syncer/web.go

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,69 @@ import (
66
"github.com/gofiber/recover"
77
"github.com/l3uddz/crop/cache"
88
"github.com/l3uddz/crop/rclone"
9+
"github.com/phayes/freeport"
910
"github.com/sirupsen/logrus"
11+
"sync"
1012
)
1113

1214
type WebServer struct {
13-
Host string
14-
Port int
15+
Host string
16+
Port int
17+
app *fiber.App
18+
log *logrus.Entry
19+
syncerName string
20+
sa *rclone.ServiceAccountManager
21+
}
1522

16-
app *fiber.App
17-
log *logrus.Entry
18-
sa *rclone.ServiceAccountManager
23+
type FreePortCache struct {
24+
pCache map[int]int
25+
sync.Mutex
1926
}
2027

2128
type ServiceAccountRequest struct {
2229
OldServiceAccount string `json:"old"`
2330
Remote string `json:"remote"`
2431
}
2532

26-
func newWebServer(host string, port int, log *logrus.Entry, sa *rclone.ServiceAccountManager) *WebServer {
33+
var (
34+
fpc *FreePortCache
35+
)
36+
37+
func init() {
38+
fpc = &FreePortCache{
39+
pCache: make(map[int]int),
40+
Mutex: sync.Mutex{},
41+
}
42+
}
43+
44+
func newWebServer(host string, log *logrus.Entry, syncerName string, sa *rclone.ServiceAccountManager) *WebServer {
45+
// get free port
46+
fpc.Lock()
47+
defer fpc.Unlock()
48+
port := 0
49+
50+
for {
51+
p, err := freeport.GetFreePort()
52+
if err != nil {
53+
log.WithError(err).Fatal("Failed locating free port for the service account server")
54+
}
55+
56+
if _, exists := fpc.pCache[p]; !exists {
57+
fpc.pCache[p] = p
58+
port = p
59+
log.Debugf("Found free port for service account server: %d", port)
60+
break
61+
}
62+
}
63+
64+
// create ws object
2765
ws := &WebServer{
28-
Host: host,
29-
Port: port,
30-
app: fiber.New(),
31-
log: log,
32-
sa: sa,
66+
Host: host,
67+
Port: port,
68+
app: fiber.New(),
69+
log: log,
70+
syncerName: syncerName,
71+
sa: sa,
3372
}
3473

3574
// setup app

0 commit comments

Comments
 (0)