netutil

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2020 License: MIT Imports: 5 Imported by: 0

README

netutil

netutil provides small helpers for implementing servers in Go.

License

Copyright © 2020 Ferdinand Hofherr

Distributed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAddr

func GetAddr(t *testing.T, addrC <-chan string, timeout time.Duration) string

GetAddr reads a servers address from addrC.

func GetErr

func GetErr(t *testing.T, errC <-chan error, timeout time.Duration) error

GetErr reads an error from errC.

func ListenAndServe

func ListenAndServe(s Server, options ...Option) error

ListenAndServe creates a net.Listener and for addr and calls s.Serve with it.

Its behavior is very similar to net/http.ListenAndServe. In contrast to net/http.ListenAndServe it does not return net/http.ErrServerClosed when the s was closed. Instead it returns nil. Errors are only returned if an error occurred.

Callers may pass any of the following options to ListenAndServe:

  • NotifyAddr
  • WithAddr
  • WithListener

See the documentation of the individual options for details. If the same option is passed more than once, the latest passed option wins. Unsupported options are silently ignored.

If neither UseListener nor UseAddr are passed the server listens on 127.0.0.1:0 by default, i.e. it chooses a random port and listens on the loopback interface only.

ListenAndServe blocks the current go routine.

Types

type Option

type Option func(*netutilOptions)

Option represents a configurable option for the helpers in netutil.

func NotifyAddr

func NotifyAddr(c chan<- string) Option

NotifyAddr passes a channel to ListenAndServe which is used to send the address the Server is listening on to the caller.

func WithAddr

func WithAddr(addr string) Option

WithAddr sets the network address the server started by ListenAndServe will listen on.

func WithListener

func WithListener(l net.Listener) Option

WithListener tells ListenAndServe to use the passed listener instead of creating a new one.

type Server

type Server interface {
	Serve(net.Listener) error
}

Server wraps the Serve method found on the http.Server and grpc.Server types.

Jump to

Keyboard shortcuts

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