github.com/cloudwego/hertz@v0.9.3/pkg/common/config/request_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 "time"
    20  
    21  var preDefinedOpts []RequestOption
    22  
    23  type RequestOptions struct {
    24  	tags map[string]string
    25  	isSD bool
    26  
    27  	dialTimeout  time.Duration
    28  	readTimeout  time.Duration
    29  	writeTimeout time.Duration
    30  	// Request timeout. Usually set by DoDeadline or DoTimeout
    31  	// if <= 0, means not set
    32  	requestTimeout time.Duration
    33  	start          time.Time
    34  }
    35  
    36  // RequestOption is the only struct to set request-level options.
    37  type RequestOption struct {
    38  	F func(o *RequestOptions)
    39  }
    40  
    41  // NewRequestOptions create a *RequestOptions according to the given opts.
    42  func NewRequestOptions(opts []RequestOption) *RequestOptions {
    43  	options := &RequestOptions{
    44  		tags: make(map[string]string),
    45  		isSD: false,
    46  	}
    47  	if preDefinedOpts != nil {
    48  		options.Apply(preDefinedOpts)
    49  	}
    50  	options.Apply(opts)
    51  	return options
    52  }
    53  
    54  // WithTag set tag in RequestOptions.
    55  func WithTag(k, v string) RequestOption {
    56  	return RequestOption{F: func(o *RequestOptions) {
    57  		o.tags[k] = v
    58  	}}
    59  }
    60  
    61  // WithSD set isSD in RequestOptions.
    62  func WithSD(b bool) RequestOption {
    63  	return RequestOption{F: func(o *RequestOptions) {
    64  		o.isSD = b
    65  	}}
    66  }
    67  
    68  // WithDialTimeout sets dial timeout.
    69  //
    70  // This is the request level configuration. It has a higher
    71  // priority than the client level configuration
    72  // Note: it won't take effect in the case of the number of
    73  // connections in the connection pool exceeds the maximum
    74  // number of connections and needs to establish a connection
    75  // while waiting.
    76  func WithDialTimeout(t time.Duration) RequestOption {
    77  	return RequestOption{F: func(o *RequestOptions) {
    78  		o.dialTimeout = t
    79  	}}
    80  }
    81  
    82  // WithReadTimeout sets read timeout.
    83  //
    84  // This is the request level configuration. It has a higher
    85  // priority than the client level configuration
    86  func WithReadTimeout(t time.Duration) RequestOption {
    87  	return RequestOption{F: func(o *RequestOptions) {
    88  		o.readTimeout = t
    89  	}}
    90  }
    91  
    92  // WithWriteTimeout sets write timeout.
    93  //
    94  // This is the request level configuration. It has a higher
    95  // priority than the client level configuration
    96  func WithWriteTimeout(t time.Duration) RequestOption {
    97  	return RequestOption{F: func(o *RequestOptions) {
    98  		o.writeTimeout = t
    99  	}}
   100  }
   101  
   102  // WithRequestTimeout sets whole request timeout. If it reaches timeout,
   103  // the client will return.
   104  //
   105  // This is the request level configuration.
   106  func WithRequestTimeout(t time.Duration) RequestOption {
   107  	return RequestOption{F: func(o *RequestOptions) {
   108  		o.requestTimeout = t
   109  	}}
   110  }
   111  
   112  func (o *RequestOptions) Apply(opts []RequestOption) {
   113  	for _, op := range opts {
   114  		op.F(o)
   115  	}
   116  }
   117  
   118  func (o *RequestOptions) Tag(k string) string {
   119  	return o.tags[k]
   120  }
   121  
   122  func (o *RequestOptions) Tags() map[string]string {
   123  	return o.tags
   124  }
   125  
   126  func (o *RequestOptions) IsSD() bool {
   127  	return o.isSD
   128  }
   129  
   130  func (o *RequestOptions) DialTimeout() time.Duration {
   131  	return o.dialTimeout
   132  }
   133  
   134  func (o *RequestOptions) ReadTimeout() time.Duration {
   135  	return o.readTimeout
   136  }
   137  
   138  func (o *RequestOptions) WriteTimeout() time.Duration {
   139  	return o.writeTimeout
   140  }
   141  
   142  func (o *RequestOptions) RequestTimeout() time.Duration {
   143  	return o.requestTimeout
   144  }
   145  
   146  // StartRequest records the start time of the request.
   147  //
   148  // Note: Users should not call this method.
   149  func (o *RequestOptions) StartRequest() {
   150  	if o.requestTimeout > 0 {
   151  		o.start = time.Now()
   152  	}
   153  }
   154  
   155  func (o *RequestOptions) StartTime() time.Time {
   156  	return o.start
   157  }
   158  
   159  func (o *RequestOptions) CopyTo(dst *RequestOptions) {
   160  	if dst.tags == nil {
   161  		dst.tags = make(map[string]string)
   162  	}
   163  
   164  	for k, v := range o.tags {
   165  		dst.tags[k] = v
   166  	}
   167  
   168  	dst.isSD = o.isSD
   169  	dst.readTimeout = o.readTimeout
   170  	dst.writeTimeout = o.writeTimeout
   171  	dst.dialTimeout = o.dialTimeout
   172  	dst.requestTimeout = o.requestTimeout
   173  	dst.start = o.start
   174  }
   175  
   176  // SetPreDefinedOpts Pre define some RequestOption here
   177  func SetPreDefinedOpts(opts ...RequestOption) {
   178  	preDefinedOpts = nil
   179  	preDefinedOpts = append(preDefinedOpts, opts...)
   180  }