blob: da523118dfc85f6757966546b40315a8bacd2413 [file] [log] [blame]
Serge Bazanski549b72b2021-01-07 14:54:19 +01001// Copyright 2020 The Monogon Project Authors.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17package freeport
18
19import (
20 "io"
21 "net"
22)
23
Serge Bazanski216fe7b2021-05-21 18:36:16 +020024// AllocateTCPPort allocates a TCP port on the looopback address, and starts a
25// temporary listener on it. That listener is returned to the caller alongside with
26// the allocated port number. The listener must be closed right before the port is
27// used by the caller. This naturally still leaves a race condition window where
28// that port number might be snatched up by some other process, but there doesn't
29// seem to be a better way to do this.
Serge Bazanski549b72b2021-01-07 14:54:19 +010030func AllocateTCPPort() (uint16, io.Closer, error) {
31 addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:0")
32 if err != nil {
33 return 0, nil, err
34 }
35
36 l, err := net.ListenTCP("tcp", addr)
37 if err != nil {
38 return 0, nil, err
39 }
40 return uint16(l.Addr().(*net.TCPAddr).Port), l, nil
41}
42
Serge Bazanski216fe7b2021-05-21 18:36:16 +020043// MustConsume takes the result of AllocateTCPPort, closes the listener and returns
44// the allocated port. If anything goes wrong (port could not be allocated or
45// closed) it will panic.
Serge Bazanski549b72b2021-01-07 14:54:19 +010046func MustConsume(port uint16, lis io.Closer, err error) int {
47 if err != nil {
48 panic(err)
49 }
50 if err := lis.Close(); err != nil {
51 panic(err)
52 }
53 return int(port)
54}