Serge Bazanski | 549b72b | 2021-01-07 14:54:19 +0100 | [diff] [blame] | 1 | // 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 | |
| 17 | package freeport |
| 18 | |
| 19 | import ( |
| 20 | "io" |
| 21 | "net" |
| 22 | ) |
| 23 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 24 | // 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 Bazanski | 549b72b | 2021-01-07 14:54:19 +0100 | [diff] [blame] | 30 | func 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 Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame^] | 43 | // 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 Bazanski | 549b72b | 2021-01-07 14:54:19 +0100 | [diff] [blame] | 46 | func 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 | } |