client

package
v0.0.0-...-be384fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultHTTPClient

func DefaultHTTPClient(remoteAddr string) (*http.Client, error)

DefaultHTTPClient is used to create an http client with some default parameters. We overwrite the http.Client.Dial so we can do http over tcp or unix. remoteAddr should be fully featured (eg. with tcp:// or unix://). An error will be returned in case of invalid remoteAddr.

Types

type Caller

type Caller interface {
	// Call sends a new request for method to the server with the given
	// parameters. If params == nil, the request has empty parameters.
	// If result == nil, any result value must be discarded without error.
	// Otherwise the concrete value of result must be a pointer.
	Call(ctx context.Context, method string, params, result interface{}) error
}

A Caller handles the round trip of a single JSON-RPC request. The implementation is responsible for assigning request IDs, marshaling parameters, and unmarshaling results.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a JSON-RPC client, which sends POST HTTP requests to the remote server.

Client is safe for concurrent use by multiple goroutines.

func New

func New(remote string) (*Client, error)

New returns a Client pointed at the given address. An error is returned on invalid remote. The function panics when remote is nil.

func NewWithHTTPClient

func NewWithHTTPClient(remote string, c *http.Client) (*Client, error)

NewWithHTTPClient returns a Client pointed at the given address using a custom HTTP client. It reports an error if c == nil or if remote is not a valid URL.

func (*Client) Call

func (c *Client) Call(ctx context.Context, method string, params, result interface{}) error

Call issues a POST HTTP request. Requests are JSON encoded. Content-Type: application/json.

func (*Client) NewRequestBatch

func (c *Client) NewRequestBatch() *RequestBatch

NewRequestBatch starts a batch of requests for this client.

type RequestBatch

type RequestBatch struct {
	// contains filtered or unexported fields
}

RequestBatch allows us to buffer multiple request/response structures into a single batch request. Note that this batch acts like a FIFO queue, and is thread-safe.

func (*RequestBatch) Call

func (b *RequestBatch) Call(_ context.Context, method string, params, result interface{}) error

Call enqueues a request to call the given RPC method with the specified parameters, in the same way that the `Client.Call` function would.

func (*RequestBatch) Clear

func (b *RequestBatch) Clear() int

Clear empties out the request batch.

func (*RequestBatch) Count

func (b *RequestBatch) Count() int

Count returns the number of enqueued requests waiting to be sent.

func (*RequestBatch) Send

func (b *RequestBatch) Send(ctx context.Context) ([]interface{}, error)

Send will attempt to send the current batch of enqueued requests, and then will clear out the requests once done. On success, this returns the deserialized list of results from each of the enqueued requests.

type WSClient

type WSClient struct {
	Logger log.Logger

	Address       string // IP:PORT or /path/to/socket
	Endpoint      string // /websocket/addr/endpoint
	DialerContext func(context.Context, string, string) (net.Conn, error)

	// Single user facing channel to read RPCResponses from, closed only when the
	// client is being stopped.
	ResponsesCh chan rpctypes.RPCResponse
	// contains filtered or unexported fields
}

WSClient is a JSON-RPC client, which uses WebSocket for communication with the remote server.

WSClient is safe for concurrent use by multiple goroutines.

func NewWS

func NewWS(remoteAddr, endpoint string) (*WSClient, error)

NewWS returns a new client with default options. The endpoint argument must begin with a `/`. An error is returned on invalid remote.

func (*WSClient) Call

func (c *WSClient) Call(ctx context.Context, method string, params map[string]interface{}) error

Call enqueues a call request onto the Send queue. Requests are JSON encoded.

func (*WSClient) IsReconnecting

func (c *WSClient) IsReconnecting() bool

IsReconnecting returns true if the client is reconnecting right now.

func (*WSClient) OnReconnect

func (c *WSClient) OnReconnect(cb func())

OnReconnect sets the callback, which will be called every time after successful reconnect. Could only be set before Start.

func (*WSClient) Send

func (c *WSClient) Send(ctx context.Context, request rpctypes.RPCRequest) error

Send the given RPC request to the server. Results will be available on ResponsesCh, errors, if any, on ErrorsCh. Will block until send succeeds or ctx.Done is closed.

func (*WSClient) Start

func (c *WSClient) Start(ctx context.Context) error

Start dials the specified service address and starts the I/O routines. The service routines run until ctx terminates. To wait for the client to exit after ctx ends, call Stop.

func (*WSClient) Stop

func (c *WSClient) Stop() error

Stop blocks until the client is shut down and returns nil.

TODO(creachadair): This method exists for compatibility with the original service plumbing. Give it a better name (e.g., Wait).

func (*WSClient) String

func (c *WSClient) String() string

String returns WS client full address.

func (*WSClient) Subscribe

func (c *WSClient) Subscribe(ctx context.Context, query string) error

Subscribe to a query. Note the server must have a "subscribe" route defined.

func (*WSClient) Unsubscribe

func (c *WSClient) Unsubscribe(ctx context.Context, query string) error

Unsubscribe from a query. Note the server must have a "unsubscribe" route defined.

func (*WSClient) UnsubscribeAll

func (c *WSClient) UnsubscribeAll(ctx context.Context) error

UnsubscribeAll from all. Note the server must have a "unsubscribe_all" route defined.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL