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

     1  // Package ais provides core functionality for the AIStore object storage.
     2  /*
     3   * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package ais
     6  
     7  import (
     8  	"net/http"
     9  
    10  	"github.com/NVIDIA/aistore/cmn"
    11  	"github.com/NVIDIA/aistore/cmn/cos"
    12  )
    13  
    14  type global struct {
    15  	netServ struct {
    16  		pub     *netServer
    17  		control *netServer
    18  		data    *netServer
    19  		pub2    *netServer
    20  	}
    21  	client struct {
    22  		control *http.Client // http client for intra-cluster comm
    23  		data    *http.Client // http client to execute target <=> target GET & PUT (object)
    24  	}
    25  }
    26  
    27  var g global
    28  
    29  func handlePub(path string, handler func(http.ResponseWriter, *http.Request)) {
    30  	for _, v := range allHTTPverbs {
    31  		g.netServ.pub.muxers[v].HandleFunc(path, handler)
    32  		if !cos.IsLastB(path, '/') {
    33  			g.netServ.pub.muxers[v].HandleFunc(path+"/", handler)
    34  		}
    35  	}
    36  }
    37  
    38  func handleControl(path string, handler func(http.ResponseWriter, *http.Request)) {
    39  	for _, v := range allHTTPverbs {
    40  		g.netServ.control.muxers[v].HandleFunc(path, handler)
    41  		if !cos.IsLastB(path, '/') {
    42  			g.netServ.control.muxers[v].HandleFunc(path+"/", handler)
    43  		}
    44  	}
    45  }
    46  
    47  func handleData(path string, handler func(http.ResponseWriter, *http.Request)) {
    48  	for _, v := range allHTTPverbs {
    49  		g.netServ.data.muxers[v].HandleFunc(path, handler)
    50  		if !cos.IsLastB(path, '/') {
    51  			g.netServ.data.muxers[v].HandleFunc(path+"/", handler)
    52  		}
    53  	}
    54  }
    55  
    56  func initCtrlClient(config *cmn.Config) {
    57  	const (
    58  		defaultControlWriteBufferSize = 16 * cos.KiB // for more defaults see cmn/network.go
    59  		defaultControlReadBufferSize  = 16 * cos.KiB
    60  	)
    61  	cargs := cmn.TransportArgs{
    62  		Timeout:         config.Client.Timeout.D(),
    63  		WriteBufferSize: defaultControlWriteBufferSize,
    64  		ReadBufferSize:  defaultControlReadBufferSize,
    65  	}
    66  	if config.Net.HTTP.UseHTTPS {
    67  		g.client.control = cmn.NewIntraClientTLS(cargs, config)
    68  	} else {
    69  		g.client.control = cmn.NewClient(cargs)
    70  	}
    71  }
    72  
    73  // wbuf/rbuf - when not configured use AIS defaults (to override the usual 4KB)
    74  func initDataClient(config *cmn.Config) {
    75  	wbuf, rbuf := config.Net.HTTP.WriteBufferSize, config.Net.HTTP.ReadBufferSize
    76  	if wbuf == 0 {
    77  		wbuf = cmn.DefaultWriteBufferSize
    78  	}
    79  	if rbuf == 0 {
    80  		rbuf = cmn.DefaultReadBufferSize
    81  	}
    82  	cargs := cmn.TransportArgs{
    83  		Timeout:         config.Client.TimeoutLong.D(),
    84  		WriteBufferSize: wbuf,
    85  		ReadBufferSize:  rbuf,
    86  	}
    87  	if config.Net.HTTP.UseHTTPS {
    88  		g.client.data = cmn.NewIntraClientTLS(cargs, config)
    89  	} else {
    90  		g.client.data = cmn.NewClient(cargs)
    91  	}
    92  }
    93  
    94  func shuthttp() {
    95  	config := cmn.GCO.Get()
    96  	g.netServ.pub.shutdown(config)
    97  	if g.netServ.pub2 != nil {
    98  		g.netServ.pub2.shutdown(config)
    99  	}
   100  	if config.HostNet.UseIntraControl {
   101  		g.netServ.control.shutdown(config)
   102  	}
   103  	if config.HostNet.UseIntraData {
   104  		g.netServ.data.shutdown(config)
   105  	}
   106  }