net

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Overview

Package net implements functions for running network servers.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartAcceptLoop

func StartAcceptLoop(l net.Listener, rOpts retry.Options) (<-chan net.Conn, <-chan error)

StartAcceptLoop starts an accept loop for the given listener, returning accepted connections via a channel while handling temporary network errors. Fatal errors are returned via the error channel with the listener closed on return.

func StartForeverAcceptLoop

func StartForeverAcceptLoop(l net.Listener, rOpts retry.Options) (<-chan net.Conn, <-chan error)

StartForeverAcceptLoop starts an accept loop for the given listener that retries forever, returning accepted connections via a channel while handling temporary network errors. Fatal errors are returned via the error channel with the listener closed on return.

Example
package main

import (
	"io"
	"log"
	"net"

	xnet "github.com/m3db/m3/src/x/net"
	"github.com/m3db/m3/src/x/retry"
)

func main() {
	// Create a new listener.
	l, err := net.Listen("tcp", ":0")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	// Start accepting incoming connections.
	var (
		opts          = retry.NewOptions()
		connCh, errCh = xnet.StartAcceptLoop(l, opts)
	)

	for {
		select {
		case conn := <-connCh:
			// Handle the connection in a new goroutine.
			go func(c net.Conn) {
				io.Copy(c, c)
				c.Close()
			}(conn)

		case err := <-errCh:
			// Only fatal errors are returned on errCh.
			log.Fatal(err)
		}
	}
}
Output:

Types

type ListenerOption added in v0.15.0

type ListenerOption func(o *ListenerOptions)

ListenerOption is a listener option that can be applied to a set of listener options.

func ListenerReusePort added in v0.15.0

func ListenerReusePort(value bool) ListenerOption

ListenerReusePort is a listener option to reuse ports.

type ListenerOptions added in v0.15.0

type ListenerOptions struct {
	// contains filtered or unexported fields
}

ListenerOptions is a set of listener options that can create listeners.

func NewListenerOptions added in v0.15.0

func NewListenerOptions(opts ...ListenerOption) ListenerOptions

NewListenerOptions creates a set of listener options with supplied options.

func (ListenerOptions) Listen added in v0.15.0

func (o ListenerOptions) Listen(
	protocol, address string,
) (gonet.Listener, error)

Listen creates a new listener based on options.

Directories

Path Synopsis
cors
Package cors handles cross-origin HTTP requests (CORS).
Package cors handles cross-origin HTTP requests (CORS).

Jump to

Keyboard shortcuts

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