net

package
v0.0.0-...-9649366 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

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

This section is empty.

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