github.com/theQRL/go-zond@v0.1.1/rpc/client_opt.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package rpc
    18  
    19  import (
    20  	"net/http"
    21  
    22  	"github.com/gorilla/websocket"
    23  )
    24  
    25  // ClientOption is a configuration option for the RPC client.
    26  type ClientOption interface {
    27  	applyOption(*clientConfig)
    28  }
    29  
    30  type clientConfig struct {
    31  	// HTTP settings
    32  	httpClient  *http.Client
    33  	httpHeaders http.Header
    34  	httpAuth    HTTPAuth
    35  
    36  	// WebSocket options
    37  	wsDialer *websocket.Dialer
    38  
    39  	// RPC handler options
    40  	idgen              func() ID
    41  	batchItemLimit     int
    42  	batchResponseLimit int
    43  }
    44  
    45  func (cfg *clientConfig) initHeaders() {
    46  	if cfg.httpHeaders == nil {
    47  		cfg.httpHeaders = make(http.Header)
    48  	}
    49  }
    50  
    51  func (cfg *clientConfig) setHeader(key, value string) {
    52  	cfg.initHeaders()
    53  	cfg.httpHeaders.Set(key, value)
    54  }
    55  
    56  type optionFunc func(*clientConfig)
    57  
    58  func (fn optionFunc) applyOption(opt *clientConfig) {
    59  	fn(opt)
    60  }
    61  
    62  // WithWebsocketDialer configures the websocket.Dialer used by the RPC client.
    63  func WithWebsocketDialer(dialer websocket.Dialer) ClientOption {
    64  	return optionFunc(func(cfg *clientConfig) {
    65  		cfg.wsDialer = &dialer
    66  	})
    67  }
    68  
    69  // WithHeader configures HTTP headers set by the RPC client. Headers set using this option
    70  // will be used for both HTTP and WebSocket connections.
    71  func WithHeader(key, value string) ClientOption {
    72  	return optionFunc(func(cfg *clientConfig) {
    73  		cfg.initHeaders()
    74  		cfg.httpHeaders.Set(key, value)
    75  	})
    76  }
    77  
    78  // WithHeaders configures HTTP headers set by the RPC client. Headers set using this
    79  // option will be used for both HTTP and WebSocket connections.
    80  func WithHeaders(headers http.Header) ClientOption {
    81  	return optionFunc(func(cfg *clientConfig) {
    82  		cfg.initHeaders()
    83  		for k, vs := range headers {
    84  			cfg.httpHeaders[k] = vs
    85  		}
    86  	})
    87  }
    88  
    89  // WithHTTPClient configures the http.Client used by the RPC client.
    90  func WithHTTPClient(c *http.Client) ClientOption {
    91  	return optionFunc(func(cfg *clientConfig) {
    92  		cfg.httpClient = c
    93  	})
    94  }
    95  
    96  // WithHTTPAuth configures HTTP request authentication. The given provider will be called
    97  // whenever a request is made. Note that only one authentication provider can be active at
    98  // any time.
    99  func WithHTTPAuth(a HTTPAuth) ClientOption {
   100  	if a == nil {
   101  		panic("nil auth")
   102  	}
   103  	return optionFunc(func(cfg *clientConfig) {
   104  		cfg.httpAuth = a
   105  	})
   106  }
   107  
   108  // A HTTPAuth function is called by the client whenever a HTTP request is sent.
   109  // The function must be safe for concurrent use.
   110  //
   111  // Usually, HTTPAuth functions will call h.Set("authorization", "...") to add
   112  // auth information to the request.
   113  type HTTPAuth func(h http.Header) error
   114  
   115  // WithBatchItemLimit changes the maximum number of items allowed in batch requests.
   116  //
   117  // Note: this option applies when processing incoming batch requests. It does not affect
   118  // batch requests sent by the client.
   119  func WithBatchItemLimit(limit int) ClientOption {
   120  	return optionFunc(func(cfg *clientConfig) {
   121  		cfg.batchItemLimit = limit
   122  	})
   123  }
   124  
   125  // WithBatchResponseSizeLimit changes the maximum number of response bytes that can be
   126  // generated for batch requests. When this limit is reached, further calls in the batch
   127  // will not be processed.
   128  //
   129  // Note: this option applies when processing incoming batch requests. It does not affect
   130  // batch requests sent by the client.
   131  func WithBatchResponseSizeLimit(sizeLimit int) ClientOption {
   132  	return optionFunc(func(cfg *clientConfig) {
   133  		cfg.batchResponseLimit = sizeLimit
   134  	})
   135  }