github.com/blend/go-sdk@v1.20220411.3/proxyprotocol/create_listener.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package proxyprotocol
     9  
    10  import (
    11  	"crypto/tls"
    12  	"net"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/webutil"
    16  )
    17  
    18  // CreateListener creates a new proxy protocol listener.
    19  func CreateListener(network, addr string, opts ...CreateListenerOption) (net.Listener, error) {
    20  	ln, err := net.Listen(network, addr)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	options := CreateListenerOptions{
    25  		KeepAlive:       true,
    26  		KeepAlivePeriod: 3 * time.Minute,
    27  	}
    28  	for _, opt := range opts {
    29  		if err := opt(&options); err != nil {
    30  			return nil, err
    31  		}
    32  	}
    33  
    34  	var output net.Listener = webutil.TCPKeepAliveListener{TCPListener: ln.(*net.TCPListener)}
    35  
    36  	if options.UseProxyProtocol {
    37  		output = &Listener{Listener: output}
    38  	}
    39  	if options.TLSConfig != nil {
    40  		output = tls.NewListener(output, options.TLSConfig)
    41  	}
    42  
    43  	return output, nil
    44  }