Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // RetryOnAnyError retries on any error except for the following: // -32700: Parse error. // -32600: Invalid request. // -32601: Method not found. // -32602: Invalid params. RetryOnAnyError = func(err error) bool { switch errorCode(err) { case -32700: return false case -32600: return false case -32601: return false case -32602: return false } return err != nil } // RetryOnLimitExceeded retries on the following errors: // -32005: Limit exceeded. // 429: Too many requests. RetryOnLimitExceeded = func(err error) bool { switch errorCode(err) { case -32005: return true case 429: return true } return false } )
var ( // LinearBackoff returns a BackoffFunc that returns a constant delay. LinearBackoff = func(delay time.Duration) func(int) time.Duration { return func(_ int) time.Duration { return delay } } // ExponentialBackoff returns a BackoffFunc that returns an exponential delay. // The delay is calculated as BaseDelay * ExponentialFactor ^ retryCount. ExponentialBackoff = func(opts ExponentialBackoffOptions) func(int) time.Duration { return func(retryCount int) time.Duration { d := time.Duration(float64(opts.BaseDelay) * math.Pow(opts.ExponentialFactor, float64(retryCount))) if d > opts.MaxDelay { return opts.MaxDelay } return d } } )
var ErrNotSubscriptionTransport = errors.New("transport does not implement SubscriptionTransport")
Functions ¶
This section is empty.
Types ¶
type Combined ¶
type Combined struct {
// contains filtered or unexported fields
}
Combined is transport that uses separate transports for regular calls and subscriptions.
It is recommended by some RPC providers to use HTTP for regular calls and WebSockets for subscriptions.
func NewCombined ¶
func NewCombined(call Transport, subscriber SubscriptionTransport) *Combined
NewCombined creates a new Combined transport.
type ExponentialBackoffOptions ¶
type ExponentialBackoffOptions struct { // BaseDelay is the base delay before the first retry. BaseDelay time.Duration // MaxDelay is the maximum delay between retries. MaxDelay time.Duration // ExponentialFactor is the exponential factor to use for calculating the delay. // The delay is calculated as BaseDelay * ExponentialFactor ^ retryCount. ExponentialFactor float64 }
ExponentialBackoffOptions contains options for the ExponentialBackoff function.
type HTTP ¶
type HTTP struct {
// contains filtered or unexported fields
}
HTTP is a Transport implementation that uses the HTTP protocol.
type HTTPError ¶
type HTTPError struct { Code int // Code is the HTTP status code. Err error // Err is an optional underlying error. }
HTTPError is an HTTP error.
type HTTPOptions ¶
type HTTPOptions struct { // URL of the HTTP endpoint. URL string // HTTPClient is the HTTP client to use. If nil, http.DefaultClient is // used. HTTPClient *http.Client // HTTPHeader specifies the HTTP headers to send with each request. HTTPHeader http.Header }
HTTPOptions contains options for the HTTP transport.
type IPC ¶
type IPC struct {
// contains filtered or unexported fields
}
IPC is a Transport implementation that uses the IPC protocol.
type IPCOptions ¶
type IPCOptions struct { // Context used to close the connection. Context context.Context // Path is the path to the IPC socket. Path string // Timeout is the timeout for the IPC requests. Default is 60s. Timout time.Duration // ErrorCh is an optional channel used to report errors. ErrorCh chan error }
IPCOptions contains options for the IPC transport.
type RPCError ¶
type RPCError struct { Code int // Code is the JSON-RPC error code. Message string // Message is the error message. Data any // Data associated with the error. }
RPCError is an JSON-RPC error.
type Retry ¶
type Retry struct {
// contains filtered or unexported fields
}
Retry is a wrapper around another transport that retries requests.
func NewRetry ¶
func NewRetry(opts RetryOptions) (*Retry, error)
NewRetry creates a new Retry instance.
type RetryOptions ¶
type RetryOptions struct { // Transport is the underlying transport to use. Transport Transport // RetryFunc is a function that returns true if the request should be // retried. The RetryOnAnyError and RetryOnLimitExceeded functions can be // used or a custom function can be provided. RetryFunc func(error) bool // BackoffFunc is a function that returns the delay before the next retry. // It takes the current retry count as an argument. BackoffFunc func(int) time.Duration // MaxRetries is the maximum number of retries. If negative, there is no limit. MaxRetries int }
RetryOptions contains options for the Retry transport.
type SubscriptionTransport ¶
type SubscriptionTransport interface { Transport // Subscribe starts a new subscription. It returns a channel that receives // subscription messages and a subscription ID. Subscribe(ctx context.Context, method string, args ...any) (ch chan json.RawMessage, id string, err error) // Unsubscribe cancels a subscription. The channel returned by Subscribe // will be closed. Unsubscribe(ctx context.Context, id string) error }
SubscriptionTransport is transport that supports subscriptions.
type Transport ¶
type Transport interface { // Call performs a JSON-RPC call. Call(ctx context.Context, result any, method string, args ...any) error }
Transport handles the transport layer of the JSON-RPC protocol.
type Websocket ¶
type Websocket struct {
// contains filtered or unexported fields
}
Websocket is a Transport implementation that uses the websocket protocol.
func NewWebsocket ¶
func NewWebsocket(opts WebsocketOptions) (*Websocket, error)
NewWebsocket creates a new Websocket instance.
type WebsocketOptions ¶
type WebsocketOptions struct { // Context used to close the connection. Context context.Context // URL of the websocket endpoint. URL string // HTTPClient is the HTTP client to use. If nil, http.DefaultClient is // used. HTTPClient *http.Client // HTTPHeader specifies the HTTP headers to be included in the // websocket handshake request. HTTPHeader http.Header // Timeout is the timeout for the websocket requests. Default is 60s. Timout time.Duration // ErrorCh is an optional channel used to report errors. ErrorCh chan error }
WebsocketOptions contains options for the websocket transport.