Skip to content

Commit

Permalink
Make RequestHook storage non-atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
PlasmaPower committed Jan 18, 2023
1 parent 73434d8 commit c90128a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
6 changes: 3 additions & 3 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type BatchElem struct {

// Client represents a connection to an RPC server.
type Client struct {
RequestHook atomic.Pointer[RequestHook]
requestHook RequestHook

idgen func() ID // for subscriptions
isHTTP bool // connection type: http, ws or ipc
Expand Down Expand Up @@ -302,7 +302,7 @@ func (c *Client) CallContext(ctx context.Context, result interface{}, method str
}
op := &requestOp{ids: []json.RawMessage{msg.ID}, resp: make(chan *jsonrpcMessage, 1)}

resultHook := c.requestHook(msg)
resultHook := c.onRequest(msg)
if c.isHTTP {
err = c.sendHTTP(ctx, op, msg)
} else {
Expand Down Expand Up @@ -374,7 +374,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error {
resultHooks := make([]ResultHook, len(msgs))
responsesForHooks := make([]interface{}, len(msgs))
for i, msg := range msgs {
resultHooks[i] = c.requestHook(msg)
resultHooks[i] = c.onRequest(msg)
}

var err error
Expand Down
15 changes: 12 additions & 3 deletions rpc/client_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ func DialTransport(ctx context.Context, rawUrl string, transport *http.Transport
return rpcClient, nil
}

func DialContextWithRequestHook(ctx context.Context, url string, hook RequestHook) (*Client, error) {
client, err := DialContext(ctx, url)
if err != nil {
return nil, err
}
client.requestHook = hook
return client, nil
}

type RequestHook interface {
OnRequest(request interface{}) ResultHook
}
Expand All @@ -64,11 +73,11 @@ type noopResultHook struct{}
func (h noopResultHook) OnResult(interface{}, error) {
}

func (c *Client) requestHook(request interface{}) ResultHook {
hooks := c.RequestHook.Load()
func (c *Client) onRequest(request interface{}) ResultHook {
hooks := c.requestHook
var respHooks ResultHook
if hooks != nil {
respHooks = (*hooks).OnRequest(request)
respHooks = hooks.OnRequest(request)
}
if respHooks == nil {
respHooks = noopResultHook{}
Expand Down

0 comments on commit c90128a

Please sign in to comment.