listener

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 License: Apache-2.0 Imports: 4 Imported by: 3

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 l.Close()

	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
JackTT - Gopher 🇻🇳