Skip to content
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

fix(rpc): remove use of magic numbers in JSON service #673

Merged
merged 7 commits into from
Apr 16, 2024

Conversation

zale144
Copy link
Contributor

@zale144 zale144 commented Apr 15, 2024

Use default values for subscribe timeout and buffer size, but leave optional parameters to set them

PR Standards

Opening a pull request should be able to meet the following requirements


Close #668

<-- Briefly describe the content of this pull request -->

For Author:

  • Targeted PR against correct branch
  • included the correct type prefix in the PR title
  • Linked to Github issue with discussion and accepted design
  • Targets only one github issue
  • Wrote unit and integration tests
  • All CI checks have passed
  • Added relevant godoc comments

For Reviewer:

  • confirmed the correct type prefix in the PR title
  • Reviewers assigned
  • confirmed all author checklist items have been addressed

After reviewer approval:

  • In case targets main branch, PR should be squashed and merged.
  • In case PR targets a release branch, PR should be rebased.

@zale144 zale144 self-assigned this Apr 15, 2024
@zale144 zale144 requested a review from a team as a code owner April 15, 2024 09:24
go.mod Outdated Show resolved Hide resolved
rpc/json/service.go Outdated Show resolved Hide resolved
defer cancel()

sub, err := s.client.EventBus.Subscribe(ctx, addr, q, subBufferSize)
sub, err := s.client.EventBus.Subscribe(ctx, addr, q, s.subscribeBufferSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • why is Subscribe called directly on the EventBus and not s.client.Subscribe?
  • why we adding timeout only on the Subscribe interact and not on all other queries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't know, I was just replacing the magic numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now looking at the code,

  • s.client.Subscribe is not exactly the same as s.client.EventBus.Subscribe. The former has a different signature and does a bit more (publish events and whatnot)
  • you suggest we add it to Unsubscribe and UnsubscribeAll ? Apart from them, only TxSearch, BlockSearch, BroadcastTxCommit and BroadcastTxSync actually use the passed context

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I think all APIs should use req.Context() and not context.Background()
  2. not sure why the Subscribe method got it's own additional timeout timer. I would remove it whatsoever and apply global timeout on the RPC server

wdyt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wish the implementation didn't rely so heavily on reflection

@zale144 zale144 requested a review from mtsitrin April 15, 2024 11:41
@zale144 zale144 force-pushed the zale144/668-remove-use-of-magic-numbers branch from 21459c8 to 9c7a321 Compare April 15, 2024 15:59
rpc/server.go Dismissed Show dismissed Hide dismissed
Copy link

codecov bot commented Apr 15, 2024

Codecov Report

Attention: Patch coverage is 65.00000% with 14 lines in your changes are missing coverage. Please review.

Project coverage is 44.48%. Comparing base (6406c2e) to head (9c7a321).
Report is 17 commits behind head on main.

❗ Current head 9c7a321 differs from pull request most recent head f5371f2. Consider uploading reports for the commit f5371f2 to get more accurate results

Files Patch % Lines
rpc/json/service.go 60.00% 7 Missing and 1 partial ⚠️
rpc/server.go 70.00% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #673      +/-   ##
==========================================
+ Coverage   44.04%   44.48%   +0.43%     
==========================================
  Files         120      120              
  Lines       16531    16562      +31     
==========================================
+ Hits         7281     7367      +86     
+ Misses       8219     8165      -54     
+ Partials     1031     1030       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@danwt danwt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand, after reading func (c *Client) eventsRoutine
nice changes!

Comment on lines +32 to 36
func WithSubscribeBufferSize(size int) option {
return func(s *service) {
s.subscribeBufferSize = size
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see it's just for later

// build the base response
resultEvent := &ctypes.ResultEvent{Query: args.Query, Data: msg.Data(), Events: msg.Events()}
var resp rpctypes.RPCResponse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why the old code made a new instance resultEvent?

@@ -166,7 +160,7 @@ func (s *service) Unsubscribe(req *http.Request, args *unsubscribeArgs) (*emptyR

func (s *service) UnsubscribeAll(req *http.Request, args *unsubscribeAllArgs) (*emptyResult, error) {
s.logger.Debug("unsubscribe from all queries", "remote", req.RemoteAddr)
err := s.client.UnsubscribeAll(context.Background(), req.RemoteAddr)
err := s.client.UnsubscribeAll(req.Context(), req.RemoteAddr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 👍

defer cancel()
if err := s.server.Shutdown(ctx); err != nil {
s.Logger.Error("while shuting down RPC server", "error", err)
s.Logger.Error("while shutting down RPC server", "error", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@danwt danwt changed the title Remove use of magic numbers in JSON service fix(rpc): remove use of magic numbers in JSON service Apr 16, 2024
@mtsitrin mtsitrin merged commit cefca7a into main Apr 16, 2024
4 checks passed
@mtsitrin mtsitrin deleted the zale144/668-remove-use-of-magic-numbers branch April 16, 2024 11:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use of magic numbers decreases maintainability
3 participants