github.com/blend/go-sdk@v1.20220411.3/grpcutil/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 grpcutil
     9  
    10  import (
    11  	"net"
    12  	"strings"
    13  
    14  	"github.com/blend/go-sdk/ex"
    15  )
    16  
    17  // CreateListener creates a net listener for a given bind address.
    18  // It handles detecting if we should create a unix socket address.
    19  func CreateListener(bindAddr string) (net.Listener, error) {
    20  	var socketListener net.Listener
    21  	var err error
    22  	if strings.HasPrefix(bindAddr, "unix://") {
    23  		socketListener, err = net.Listen("unix", strings.TrimPrefix(bindAddr, "unix://"))
    24  		if typed, ok := socketListener.(*net.UnixListener); ok {
    25  			typed.SetUnlinkOnClose(true)
    26  		}
    27  	} else {
    28  		socketListener, err = net.Listen("tcp", bindAddr)
    29  	}
    30  	return socketListener, ex.New(err)
    31  }