github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/transport/client_nethttp.go (about)

     1  //go:build nethttp
     2  
     3  // Package transport provides long-lived http/tcp connections for
     4  // intra-cluster communications (see README for details and usage example).
     5  /*
     6   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     7   */
     8  package transport
     9  
    10  import (
    11  	"io"
    12  	"net/http"
    13  	"strconv"
    14  
    15  	"github.com/NVIDIA/aistore/api/apc"
    16  	"github.com/NVIDIA/aistore/cmn"
    17  	"github.com/NVIDIA/aistore/cmn/cos"
    18  	"github.com/NVIDIA/aistore/cmn/nlog"
    19  )
    20  
    21  const ua = "aisnode/streams"
    22  
    23  type Client interface {
    24  	Do(req *http.Request) (*http.Response, error)
    25  }
    26  
    27  func whichClient() string { return "net/http" }
    28  
    29  // intra-cluster networking: net/http client
    30  func NewIntraDataClient() (client *http.Client) {
    31  	config := cmn.GCO.Get()
    32  
    33  	// compare with ais/hcommon.go
    34  	wbuf, rbuf := config.Net.HTTP.WriteBufferSize, config.Net.HTTP.ReadBufferSize
    35  	if wbuf == 0 {
    36  		wbuf = cmn.DefaultWriteBufferSize
    37  	}
    38  	if rbuf == 0 {
    39  		rbuf = cmn.DefaultReadBufferSize
    40  	}
    41  	tcpbuf := config.Net.L4.SndRcvBufSize
    42  	if tcpbuf == 0 {
    43  		tcpbuf = cmn.DefaultSendRecvBufferSize
    44  	}
    45  	cargs := cmn.TransportArgs{
    46  		SndRcvBufSize:   tcpbuf,
    47  		WriteBufferSize: wbuf,
    48  		ReadBufferSize:  rbuf,
    49  	}
    50  	if config.Net.HTTP.UseHTTPS {
    51  		client = cmn.NewClientTLS(cargs, config.Net.HTTP.ToTLS())
    52  	} else {
    53  		client = cmn.NewClient(cargs)
    54  	}
    55  	return
    56  }
    57  
    58  func (s *streamBase) do(body io.Reader) (err error) {
    59  	var (
    60  		request  *http.Request
    61  		response *http.Response
    62  	)
    63  	if request, err = http.NewRequest(http.MethodPut, s.dstURL, body); err != nil {
    64  		return
    65  	}
    66  	if s.streamer.compressed() {
    67  		request.Header.Set(apc.HdrCompress, apc.LZ4Compression)
    68  	}
    69  	request.Header.Set(apc.HdrSessID, strconv.FormatInt(s.sessID, 10))
    70  	request.Header.Set(cos.HdrUserAgent, ua)
    71  
    72  	response, err = s.client.Do(request)
    73  	if err != nil {
    74  		if verbose {
    75  			nlog.Errorf("%s: Error [%v]", s, err)
    76  		}
    77  		return
    78  	}
    79  	cos.DrainReader(response.Body)
    80  	response.Body.Close()
    81  	if s.streamer.compressed() {
    82  		s.streamer.resetCompression()
    83  	}
    84  	return
    85  }