Skip to content

ethclient support rpc custom handler #33221

@RelicOfTesla

Description

@RelicOfTesla

Rationale

The ws/wss client needs to add rate limiting calls. Most third-party nodes have frequency limitations.
Or a global data convert.

(I can use "http.Client.RoundTripper.RoundTrip()" implement custom handling support for http/https in the current client code. But ws/wss/other cannot do it because the now ethclient code directly writes raw data, which is not suitable for RPC responses. )

Implementation

//case 1: 
type NextRpcHandler func(context.Context, any) error

client1 := rpc.DialOptions(ctx, target, rpc.WithRpcHandler(function(ctx context.Context, msg any, next rpc.NextRpcHandler) error{
   // todo: some rate limit code // before
   return next(ctx, msg)
   // after
}))

//case 2:
ctx = rpc.WithContextRpcHandler(ctx, function(ctx context.Context, msg any, next rpc.NextRpcHandler) error{
   // todo: some rate limit code // before
   return next(ctx, msg)
   // after
}))
client2.xxxxMethod(ctx, ...)
client2.xxxxMethod(ctx, ...)

//////////////

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions