github.com/thanos-io/thanos@v0.32.5/pkg/testutil/e2eutil/port.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package e2eutil
     5  
     6  import "net"
     7  
     8  // FreePort returns port that is free now.
     9  func FreePort() (int, error) {
    10  	addr, err := net.ResolveTCPAddr("tcp", ":0")
    11  	if err != nil {
    12  		return 0, err
    13  	}
    14  
    15  	l, err := net.ListenTCP("tcp", addr)
    16  	if err != nil {
    17  		return 0, err
    18  	}
    19  	return l.Addr().(*net.TCPAddr).Port, l.Close()
    20  }