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

Avoid unnecessary FooService allocations #390

Closed
wants to merge 2 commits into from

Conversation

mdempsky
Copy link
Contributor

Fixes #389.

@@ -107,6 +107,7 @@ type Client struct {
rateMu sync.Mutex
rateLimits [categories]Rate // Rate limits for the client as determined by the most recent API calls.
mostRecent rateLimitCategory
common service
Copy link
Member

@dmitshur dmitshur Jun 28, 2016

Choose a reason for hiding this comment

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

I have a style suggestion.

rateMu     sync.Mutex
rateLimits [categories]Rate
mostRecent rateLimitCategory

Here, rateMu is a mutex hat. Like a hat, it sits on top of the variables that it protects.

So, without needing to actually write the comment, the above is implicitly understood to be equivalent to:

// rateMu protects rateLimits and mostRecent fields.
rateMu     sync.Mutex
rateLimits [categories]Rate
mostRecent rateLimitCategory

When adding a new, unrelated field that isn't protected by rateMu, don't do this:

 rateMu     sync.Mutex
 rateLimits [categories]Rate
 mostRecent rateLimitCategory
+common     service

Instead, do this:

 rateMu     sync.Mutex
 rateLimits [categories]Rate
 mostRecent rateLimitCategory
+
+common service

How does that sound?

I can't remember where I first saw this pattern, but it might've been @davecheney. I really like it and recommend not breaking it. (I'll try to fix up the clientMu mutex on top to use this pattern too.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How does that sound?

SGTM, done.

c.Users = (*UsersService)(&c.common)
c.Licenses = (*LicensesService)(&c.common)
c.Migrations = (*MigrationService)(&c.common)
c.Reactions = (*ReactionsService)(&c.common)
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you go ahead and sort these as well? They were always supposed to be, but it looks like the last few got added to the bottom.

@dmitshur
Copy link
Member

dmitshur commented Jun 29, 2016

See comments, but otherwise I think this looks great! LGTM.

My only ask is when merging this, squash into 1 commit so the relevant commit message pointing to more details/context is fewer git-blame hops away.

@willnorris
Copy link
Collaborator

made the minor changes suggested above, squashed and merged. Thanks @mdempsky!

gmlewis pushed a commit that referenced this pull request Jul 13, 2016
This change makes the code more consistent with the rateMu below.

This is a followup to #390 (comment).

Closes #396.

Change-Id: I0fc8b280911f9171fb4d8e3fa60cf6d53b64a65b
bubg-dev pushed a commit to bubg-dev/go-github that referenced this pull request Jun 16, 2017
This change makes the code more consistent with the rateMu below.

This is a followup to google#390 (comment).

Closes google#396.

Change-Id: I0fc8b280911f9171fb4d8e3fa60cf6d53b64a65b
apollon282 added a commit to apollon282/git that referenced this pull request Feb 27, 2025
This change makes the code more consistent with the rateMu below.

This is a followup to google/go-github#390 (comment).

Closes #396.

Change-Id: I0fc8b280911f9171fb4d8e3fa60cf6d53b64a65b
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.

3 participants