listener

package module
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2025 License: Apache-2.0 Imports: 4 Imported by: 5 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

listener go GoDoc

This is a Go package to allocate a net.Listener from address candidates.

go get github.com/int128/listener

Examples

Allocate a free port

To allocate a net.Listener at a free port on localhost:

package main

import (
	"fmt"

	"github.com/int128/listener"
)

func main() {
	l, err := listener.New(nil)
	if err != nil {
		panic(err)
	}
	defer l.Close()

	fmt.Printf("Open %s", l.URL)
}
Allocate a port from candidates

To allocate a net.Listener at a port 18000 or 28000 on localhost:

package main

import (
	"fmt"

	"github.com/int128/listener"
)

func main() {
	l, err := listener.New([]string{"127.0.0.1:18000", "127.0.0.1:28000"})
	if err != nil {
		panic(err)
	}
	defer l.Close()

	fmt.Printf("Open %s", l.URL)
}

If port 18000 is already in use, it will allocate port 28000.

Contributions

This is an open source software. Free free to open issues and pull requests.

Documentation

Overview

Package listener provides utility for allocating a net.Listener from address candidates.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Listener

type Listener struct {

	// URL to the listener.
	// This is always "http://localhost:PORT" regardless of the listening address.
	URL *url.URL
	// contains filtered or unexported fields
}

Listener wraps a net.Listener and provides its URL.

func New

func New(addressCandidates []string) (*Listener, error)

New starts a Listener on one of the addresses. Caller should close the listener finally.

If nil or an empty slice is given, it defaults to "127.0.0.1:0". If multiple address are given, it will try the addresses in order.

If the port in the address is 0, it will allocate a free port.

If no port is available, it will return an error which wraps NoAvailablePortError.

Example

ExampleNew allocates a net.Listener at port 18000 or 28000.

package main

import (
	"fmt"

	"github.com/int128/listener"
)

func main() {
	l, err := listener.New([]string{"127.0.0.1:18000", "127.0.0.1:28000"})
	if err != nil {
		panic(err)
	}
	defer func() {
		if err := l.Close(); err != nil {
			panic(err)
		}
	}()

	fmt.Printf("Open %s", l.URL)

}
Output:
Open http://localhost:18000

func NewOn

func NewOn(address string) (*Listener, error)

NewOn starts a Listener on the address. Caller should close the listener finally.

If an empty string is given, it defaults to "127.0.0.1:0".

If the port in the address is 0, it will allocate a free port.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

func (*Listener) Close

func (l *Listener) Close() error

type NoAvailablePortError added in v1.1.0

type NoAvailablePortError interface {
	Unwrap() []error
}

NoAvailablePortError contains the causes of port allocation failure.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL