accepter

package module
v1.2.7 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2019 License: BSD-3-Clause Imports: 8 Imported by: 3

README

accepter

GoDoc

Package accepter provides an Accepter and utilities for net.Listener. It is similar with GoLang's http.Server.

v1.2 changes from v1.0

  • using cancelling context instead of channel
  • changed Handler.Serve arguments to (ctx, conn) from (conn, closeCh)
  • removed panic recovering for Handler.Serve(...)
  • removed Accepter.ErrorLog
  • added ListenAndServe and ListenAndServeTLS instead of TCPListenAndServe and TCPListenAndServeTLS
  • added go.mod support
  • added SetMaxTempDelay(...)

Documentation

Overview

Package accepter provides an Accepter and utilities for net.Listener.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrAlreadyServed is returned when Serve or ServeTLS method has been already called
	ErrAlreadyServed = errors.New("the accepter has already served")
)

Functions

func SetMaxTempDelay added in v1.2.3

func SetMaxTempDelay(d time.Duration)

SetMaxTempDelay sets maximum temporary error wait duration as concurrent-safe. Zero or negative values mean to wait forever. By default, zero.

Types

type Accepter

type Accepter struct {
	// Handler to invoke.
	Handler Handler

	// TLSConfig optionally provides a TLS configuration.
	TLSConfig *tls.Config
	// contains filtered or unexported fields
}

An Accepter defines parameters to accept connections. It is similar with GoLang's http.Server.

func (*Accepter) Close

func (a *Accepter) Close() (err error)

Close immediately closes the Accepter's underlying Listener and any connections. For a graceful shutdown, use Shutdown.

Close returns any error returned from closing the Accepter's underlying Listener.

func (*Accepter) ListenAndServe added in v1.2.3

func (a *Accepter) ListenAndServe(network, address string) error

ListenAndServe listens on the given network and address; and then calls Serve to handle incoming connections. ListenAndServe returns a nil error after Close or Shutdown method called.

func (*Accepter) ListenAndServeTLS added in v1.2.3

func (a *Accepter) ListenAndServeTLS(network, address string, certFile, keyFile string) error

ListenAndServeTLS listens on the given network and address; and then calls ServeTLS to handle incoming TLS connections.

Filenames containing a certificate and matching private key for the Accepter must be provided if neither the Accepter's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the Accepter's certificate, any intermediates, and the CA's certificate.

func (*Accepter) Serve

func (a *Accepter) Serve(lis net.Listener) (err error)

Serve accepts incoming connections on the Listener lis, creating a new service goroutine for each. The service goroutines read requests and then call a.Handler to reply to them. Serve always closes lis if returned error is not ErrAlreadyServed. Serve returns a nil error after Close or Shutdown method called.

func (*Accepter) ServeTLS

func (a *Accepter) ServeTLS(lis net.Listener, certFile, keyFile string) (err error)

ServeTLS accepts incoming connections on the Listener lis, creating a new service goroutine for each. The service goroutines read requests and then call a.Handler to reply to them. ServeTLS always closes lis if returned error is not ErrAlreadyServed or as TLSError. ServeTLS returns a nil error after Close or Shutdown method called.

Additionally, files containing a certificate and matching private key for the Accepter must be provided if neither the Accepter's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the Accepter's certificate, any intermediates, and the CA's certificate.

func (*Accepter) Shutdown

func (a *Accepter) Shutdown(ctx context.Context) (err error)

Shutdown gracefully shuts down the Accepter without interrupting any connections. Shutdown works by first closing the Accepter's underlying Listener, then cancels the context on Serve method of Handler, and then waiting indefinitely for connections to exit Serve method of Handler and then close. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the Accepter's underlying Listener.

When Shutdown is called, Serve, ServeTLS, ListenAndServe, and ListenAndServeTLS immediately return nil. Make sure the program doesn't exit and waits instead for Shutdown to return.

type Handler

type Handler interface {
	Serve(ctx context.Context, conn net.Conn)
}

A Handler responds to an incoming connection.

type HandlerFunc

type HandlerFunc func(ctx context.Context, conn net.Conn)

The HandlerFunc type is an adapter to allow the use of ordinary functions as handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (HandlerFunc) Serve

func (f HandlerFunc) Serve(ctx context.Context, conn net.Conn)

Serve calls f(ctx, conn)

type TLSError added in v1.2.7

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

TLSError is returned when a method fails with TLS error

func (*TLSError) Error added in v1.2.7

func (e *TLSError) Error() string

Error is implementation of error

func (*TLSError) Unwrap added in v1.2.7

func (e *TLSError) Unwrap() error

Unwrap returns wrapped error

Jump to

Keyboard shortcuts

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