github.com/m3db/m3@v1.5.0/src/x/net/server.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package net implements functions for running network servers.
    22  package net
    23  
    24  import (
    25  	"net"
    26  
    27  	"github.com/m3db/m3/src/x/errors"
    28  	"github.com/m3db/m3/src/x/retry"
    29  )
    30  
    31  // StartAcceptLoop starts an accept loop for the given listener,
    32  // returning accepted connections via a channel while handling
    33  // temporary network errors. Fatal errors are returned via the
    34  // error channel with the listener closed on return.
    35  func StartAcceptLoop(l net.Listener, rOpts retry.Options) (<-chan net.Conn, <-chan error) {
    36  	var (
    37  		connCh  = make(chan net.Conn)
    38  		errCh   = make(chan error)
    39  		retrier = retry.NewRetrier(rOpts)
    40  	)
    41  
    42  	go func() {
    43  		defer l.Close()
    44  
    45  		for {
    46  			var conn net.Conn
    47  			if err := retrier.Attempt(func() error {
    48  				var connErr error
    49  				conn, connErr = l.Accept()
    50  				if connErr == nil {
    51  					return nil
    52  				}
    53  				// If the error is a temporary network error, we consider it retryable.
    54  				if ne, ok := connErr.(net.Error); ok && ne.Temporary() {
    55  					return ne
    56  				}
    57  				// Otherwise it's a non-retryable error.
    58  				return errors.NewNonRetryableError(connErr)
    59  			}); err != nil {
    60  				close(connCh)
    61  				errCh <- err
    62  				close(errCh)
    63  				return
    64  			}
    65  			connCh <- conn
    66  		}
    67  	}()
    68  
    69  	return connCh, errCh
    70  }
    71  
    72  // StartForeverAcceptLoop starts an accept loop for the
    73  // given listener that retries forever, returning
    74  // accepted connections via a channel while handling
    75  // temporary network errors. Fatal errors are returned via the
    76  // error channel with the listener closed on return.
    77  func StartForeverAcceptLoop(l net.Listener, rOpts retry.Options) (<-chan net.Conn, <-chan error) {
    78  	return StartAcceptLoop(l, rOpts.SetForever(true))
    79  }