-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jsonrpc: small refactor, extract method for groupedJSON-RPC calls. #71
Conversation
client/jsonrpc/client.go
Outdated
func (c *rpcClient) GroupedJSONrpc( | ||
ctx context.Context, | ||
group *errgroup.Group, | ||
method string, | ||
args []any, | ||
output *bytes.Buffer, | ||
debugBlockNumber int64, | ||
) { | ||
group.Go(func() error { | ||
errCh := make(chan error, 1) | ||
c.wrkPool.Submit(func() { | ||
defer close(errCh) | ||
err := c.getResponseBody(ctx, method, args, output) | ||
if err != nil { | ||
c.log.Error("Failed to get response for jsonRPC", | ||
"blockNumber", debugBlockNumber, | ||
"method", method, | ||
"error", err, | ||
) | ||
errCh <- err | ||
} else { | ||
errCh <- nil | ||
} | ||
}) | ||
return <-errCh | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering what you think of having a function like this:
func (c *rpcClient) execOnPoolInGroup(
group *errgroup.Group,
function func() error,
) {
group.Go(func() error {
errCh := make(chan error, 1)
c.wrkPool.Submit(func() {
defer close(errCh)
err := function()
if err != nil {
errCh <- err
} else {
errCh <- nil
}
})
return <-errCh
})
}
That can be called like this:
c.execOnPoolInGroup(group, func() error {
err := c.getResponseBody(ctx, method, args, output)
if err != nil {
c.log.Error("Failed to get response for jsonRPC",
"blockNumber", debugBlockNumber,
"method", method,
"error", err,
)
}
return err
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it, but don't we end up writting more core? we always want to call a jsonRPC method and including the err handling log.
Or said in a different way, I think that is a better abstraction to hide the mess of group.Go() + wrkPool.Submit() but I also want the syntatic sugar to run the prebaked closure you wrote above.
maybe: "porque no los dos?" -- we can have both, let me do that.
extract a method for doing the grouped JSON-RPC calls
122d4d7
to
6cbb646
Compare
this is a small improvment to the current ugly code we use for:
This is ontop of #70,