Skip to content

Commit

Permalink
Dont return errors that are always nil from sync/async since these wo… (
Browse files Browse the repository at this point in the history
#115)

* Dont return errors that are always nil from sync/async since these wont ever actually have meaningful errors

* Add logs to http client

* Add log lines
  • Loading branch information
cmmarslender authored Mar 17, 2024
1 parent 79749b0 commit 7e9a1c8
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
8 changes: 4 additions & 4 deletions pkg/httpclient/httpclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,11 @@ func (c *HTTPClient) AddDisconnectHandler(onDisconnect rpcinterface.DisconnectHa
func (c *HTTPClient) AddReconnectHandler(onReconnect rpcinterface.ReconnectHandler) {}

// SetSyncMode does not apply to the HTTP Client
func (c *HTTPClient) SetSyncMode() error {
return nil
func (c *HTTPClient) SetSyncMode() {
c.logger.Debug("Sync mode is default for HTTP client. SetSyncMode call is ignored")
}

// SetAsyncMode does not apply to the HTTP Client
func (c *HTTPClient) SetAsyncMode() error {
return fmt.Errorf("async mode is not supported on the HTTP client")
func (c *HTTPClient) SetAsyncMode() {
c.logger.Debug("Async mode is not applicable to the HTTP client. SetAsyncMode call is ignored")
}
8 changes: 4 additions & 4 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ func (c *Client) AddReconnectHandler(onReconnect rpcinterface.ReconnectHandler)
// SetSyncMode sets the client to wait for responses before returning
// This is default (and only option) for HTTP client
// Websocket client defaults to async mode
func (c *Client) SetSyncMode() error {
return c.activeClient.SetSyncMode()
func (c *Client) SetSyncMode() {
c.activeClient.SetSyncMode()
}

// SetAsyncMode sets the client to async mode
// This does not apply to the HTTP client
// For the websocket client, this is the default mode and means that RPC function calls return immediate with empty
// versions of the structs that would otherwise contain the response, and you should have an async handler defined
// to receive the response
func (c *Client) SetAsyncMode() error {
return c.activeClient.SetAsyncMode()
func (c *Client) SetAsyncMode() {
c.activeClient.SetAsyncMode()
}
3 changes: 2 additions & 1 deletion pkg/rpc/clientoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func WithManualConfig(cfg config.ChiaConfig) rpcinterface.ConfigOptionFunc {
// WithSyncWebsocket is a helper to making the client and calling SetSyncMode to set the client to sync mode by default
func WithSyncWebsocket() rpcinterface.ClientOptionFunc {
return func(c rpcinterface.Client) error {
return c.SetSyncMode()
c.SetSyncMode()
return nil
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/rpcinterface/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ type Client interface {
// This is default for HTTP client, but websocket default is async, so this forces a different mode
// Note that anything received by the websocket in sync mode that is not the current expected response
// will be ignored
SetSyncMode() error
SetSyncMode()

// SetAsyncMode sets the client to async mode
// This is not supported for the HTTP client, but will set the websocket client back to async mode
// if it was set to sync mode temporarily
SetAsyncMode() error
SetAsyncMode()
}
6 changes: 2 additions & 4 deletions pkg/websocketclient/websocketclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,15 @@ func (c *WebsocketClient) AddReconnectHandler(onReconnect rpcinterface.Reconnect

// SetSyncMode enforces synchronous request/response behavior
// RPC method calls return the actual expected RPC response when this mode is enabled
func (c *WebsocketClient) SetSyncMode() error {
func (c *WebsocketClient) SetSyncMode() {
c.syncMode = true
return nil
}

// SetAsyncMode sets the client to async mode (default)
// RPC method calls return empty versions of the response objects, and you must have your own
// listeners to get the responses and handle them
func (c *WebsocketClient) SetAsyncMode() error {
func (c *WebsocketClient) SetAsyncMode() {
c.syncMode = false
return nil
}

func (c *WebsocketClient) reconnectLoop() {
Expand Down

0 comments on commit 7e9a1c8

Please sign in to comment.