MCPcopy
hub / github.com/keploy/keploy / GetAvailablePort

Function GetAvailablePort

utils/utils.go:1123–1134  ·  view source on GitHub ↗

GetAvailablePort finds and returns an available port on the system

()

Source from the content-addressed store, hash-verified

1121
1122// GetAvailablePort finds and returns an available port on the system
1123func GetAvailablePort() (uint32, error) {
1124 // Use port 0 to let the OS assign an available port
1125 listener, err := net.Listen("tcp", ":0")
1126 if err != nil {
1127 return 0, fmt.Errorf("failed to find available port: %w", err)
1128 }
1129 defer listener.Close()
1130
1131 // Extract the port from the listener's address
1132 addr := listener.Addr().(*net.TCPAddr)
1133 return uint32(addr.Port), nil
1134}
1135
1136// isPortAvailable checks if a specific port is available on the system
1137func isPortAvailable(port uint32) bool {

Callers 2

SetupMethod · 0.92
EnsureAvailablePortsFunction · 0.85

Calls 1

CloseMethod · 0.65

Tested by

no test coverage detected