github.com/ethereum/go-ethereum@v1.14.3/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 wsMessageSizeLimit *int64 // wsMessageSizeLimit nil = default, 0 = no limit 39 40 // RPC handler options 41 idgen func() ID 42 batchItemLimit int 43 batchResponseLimit int 44 } 45 46 func (cfg *clientConfig) initHeaders() { 47 if cfg.httpHeaders == nil { 48 cfg.httpHeaders = make(http.Header) 49 } 50 } 51 52 func (cfg *clientConfig) setHeader(key, value string) { 53 cfg.initHeaders() 54 cfg.httpHeaders.Set(key, value) 55 } 56 57 type optionFunc func(*clientConfig) 58 59 func (fn optionFunc) applyOption(opt *clientConfig) { 60 fn(opt) 61 } 62 63 // WithWebsocketDialer configures the websocket.Dialer used by the RPC client. 64 func WithWebsocketDialer(dialer websocket.Dialer) ClientOption { 65 return optionFunc(func(cfg *clientConfig) { 66 cfg.wsDialer = &dialer 67 }) 68 } 69 70 // WithWebsocketMessageSizeLimit configures the websocket message size limit used by the RPC 71 // client. Passing a limit of 0 means no limit. 72 func WithWebsocketMessageSizeLimit(messageSizeLimit int64) ClientOption { 73 return optionFunc(func(cfg *clientConfig) { 74 cfg.wsMessageSizeLimit = &messageSizeLimit 75 }) 76 } 77 78 // WithHeader configures HTTP headers set by the RPC client. Headers set using this option 79 // will be used for both HTTP and WebSocket connections. 80 func WithHeader(key, value string) ClientOption { 81 return optionFunc(func(cfg *clientConfig) { 82 cfg.initHeaders() 83 cfg.httpHeaders.Set(key, value) 84 }) 85 } 86 87 // WithHeaders configures HTTP headers set by the RPC client. Headers set using this 88 // option will be used for both HTTP and WebSocket connections. 89 func WithHeaders(headers http.Header) ClientOption { 90 return optionFunc(func(cfg *clientConfig) { 91 cfg.initHeaders() 92 for k, vs := range headers { 93 cfg.httpHeaders[k] = vs 94 } 95 }) 96 } 97 98 // WithHTTPClient configures the http.Client used by the RPC client. 99 func WithHTTPClient(c *http.Client) ClientOption { 100 return optionFunc(func(cfg *clientConfig) { 101 cfg.httpClient = c 102 }) 103 } 104 105 // WithHTTPAuth configures HTTP request authentication. The given provider will be called 106 // whenever a request is made. Note that only one authentication provider can be active at 107 // any time. 108 func WithHTTPAuth(a HTTPAuth) ClientOption { 109 if a == nil { 110 panic("nil auth") 111 } 112 return optionFunc(func(cfg *clientConfig) { 113 cfg.httpAuth = a 114 }) 115 } 116 117 // A HTTPAuth function is called by the client whenever a HTTP request is sent. 118 // The function must be safe for concurrent use. 119 // 120 // Usually, HTTPAuth functions will call h.Set("authorization", "...") to add 121 // auth information to the request. 122 type HTTPAuth func(h http.Header) error 123 124 // WithBatchItemLimit changes the maximum number of items allowed in batch requests. 125 // 126 // Note: this option applies when processing incoming batch requests. It does not affect 127 // batch requests sent by the client. 128 func WithBatchItemLimit(limit int) ClientOption { 129 return optionFunc(func(cfg *clientConfig) { 130 cfg.batchItemLimit = limit 131 }) 132 } 133 134 // WithBatchResponseSizeLimit changes the maximum number of response bytes that can be 135 // generated for batch requests. When this limit is reached, further calls in the batch 136 // will not be processed. 137 // 138 // Note: this option applies when processing incoming batch requests. It does not affect 139 // batch requests sent by the client. 140 func WithBatchResponseSizeLimit(sizeLimit int) ClientOption { 141 return optionFunc(func(cfg *clientConfig) { 142 cfg.batchResponseLimit = sizeLimit 143 }) 144 }