github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/admin/http_over_uds_client.go (about)

     1  package admin
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  type ClientOption func(*http.Client)
    11  
    12  // NewHTTPOverUDSClient creates a http client that communicates via UDS (unix domain sockets)
    13  func NewHTTPOverUDSClient(socketAddr string, opts ...ClientOption) (*http.Client, error) {
    14  	if socketAddr == "" {
    15  		return nil, ErrInvalidSocketPathname
    16  	}
    17  	// TODO:
    18  	// other kinds of validations?
    19  
    20  	client := &http.Client{
    21  		Transport: &http.Transport{
    22  			DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
    23  				return net.Dial("unix", socketAddr)
    24  			},
    25  		},
    26  	}
    27  
    28  	for _, opt := range opts {
    29  		opt(client)
    30  	}
    31  
    32  	return client, nil
    33  }
    34  
    35  func WithTimeout(d time.Duration) ClientOption {
    36  	return func(c *http.Client) {
    37  		c.Timeout = d
    38  	}
    39  }