github.com/cloudwego/hertz@v0.9.3/pkg/common/config/client_option.go (about)

     1  /*
     2   * Copyright 2022 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package config
    18  
    19  import (
    20  	"crypto/tls"
    21  	"time"
    22  
    23  	"github.com/cloudwego/hertz/pkg/app/client/retry"
    24  	"github.com/cloudwego/hertz/pkg/network"
    25  	"github.com/cloudwego/hertz/pkg/protocol/consts"
    26  )
    27  
    28  type ConnPoolState struct {
    29  	// The conn num of conn pool. These conns are idle connections.
    30  	PoolConnNum int
    31  	// Total conn num.
    32  	TotalConnNum int
    33  	// Number of pending connections
    34  	WaitConnNum int
    35  	// HostClient Addr
    36  	Addr string
    37  }
    38  
    39  type HostClientState interface {
    40  	ConnPoolState() ConnPoolState
    41  }
    42  
    43  type HostClientStateFunc func(HostClientState)
    44  
    45  // ClientOption is the only struct that can be used to set ClientOptions.
    46  type ClientOption struct {
    47  	F func(o *ClientOptions)
    48  }
    49  
    50  type ClientOptions struct {
    51  	// Timeout for establishing a connection to server
    52  	DialTimeout time.Duration
    53  	// The max connection nums for each host
    54  	MaxConnsPerHost int
    55  
    56  	MaxIdleConnDuration time.Duration
    57  	MaxConnDuration     time.Duration
    58  	MaxConnWaitTimeout  time.Duration
    59  	KeepAlive           bool
    60  	ReadTimeout         time.Duration
    61  	TLSConfig           *tls.Config
    62  	ResponseBodyStream  bool
    63  
    64  	// Client name. Used in User-Agent request header.
    65  	//
    66  	// Default client name is used if not set.
    67  	Name string
    68  
    69  	// NoDefaultUserAgentHeader when set to true, causes the default
    70  	// User-Agent header to be excluded from the Request.
    71  	NoDefaultUserAgentHeader bool
    72  
    73  	// Dialer is the custom dialer used to establish connection.
    74  	// Default Dialer is used if not set.
    75  	Dialer network.Dialer
    76  
    77  	// Attempt to connect to both ipv4 and ipv6 addresses if set to true.
    78  	//
    79  	// This option is used only if default TCP dialer is used,
    80  	// i.e. if Dialer is blank.
    81  	//
    82  	// By default client connects only to ipv4 addresses,
    83  	// since unfortunately ipv6 remains broken in many networks worldwide :)
    84  	DialDualStack bool
    85  
    86  	// Maximum duration for full request writing (including body).
    87  	//
    88  	// By default request write timeout is unlimited.
    89  	WriteTimeout time.Duration
    90  
    91  	// Maximum response body size.
    92  	//
    93  	// The client returns ErrBodyTooLarge if this limit is greater than 0
    94  	// and response body is greater than the limit.
    95  	//
    96  	// By default response body size is unlimited.
    97  	MaxResponseBodySize int
    98  
    99  	// Header names are passed as-is without normalization
   100  	// if this option is set.
   101  	//
   102  	// Disabled header names' normalization may be useful only for proxying
   103  	// responses to other clients expecting case-sensitive header names.
   104  	//
   105  	// By default request and response header names are normalized, i.e.
   106  	// The first letter and the first letters following dashes
   107  	// are uppercased, while all the other letters are lowercased.
   108  	// Examples:
   109  	//
   110  	//     * HOST -> Host
   111  	//     * content-type -> Content-Type
   112  	//     * cONTENT-lenGTH -> Content-Length
   113  	DisableHeaderNamesNormalizing bool
   114  
   115  	// Path values are sent as-is without normalization
   116  	//
   117  	// Disabled path normalization may be useful for proxying incoming requests
   118  	// to servers that are expecting paths to be forwarded as-is.
   119  	//
   120  	// By default path values are normalized, i.e.
   121  	// extra slashes are removed, special characters are encoded.
   122  	DisablePathNormalizing bool
   123  
   124  	// all configurations related to retry
   125  	RetryConfig *retry.Config
   126  
   127  	HostClientStateObserve HostClientStateFunc
   128  
   129  	// StateObserve execution interval
   130  	ObservationInterval time.Duration
   131  
   132  	// Callback hook for re-configuring host client
   133  	// If an error is returned, the request will be terminated.
   134  	HostClientConfigHook func(hc interface{}) error
   135  }
   136  
   137  func NewClientOptions(opts []ClientOption) *ClientOptions {
   138  	options := &ClientOptions{
   139  		DialTimeout:         consts.DefaultDialTimeout,
   140  		MaxConnsPerHost:     consts.DefaultMaxConnsPerHost,
   141  		MaxIdleConnDuration: consts.DefaultMaxIdleConnDuration,
   142  		KeepAlive:           true,
   143  		ObservationInterval: time.Second * 5,
   144  	}
   145  	options.Apply(opts)
   146  
   147  	return options
   148  }
   149  
   150  func (o *ClientOptions) Apply(opts []ClientOption) {
   151  	for _, op := range opts {
   152  		op.F(o)
   153  	}
   154  }