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

Use copy on write strategy for metadata #489

Merged
merged 11 commits into from
Jun 30, 2020
Merged

Use copy on write strategy for metadata #489

merged 11 commits into from
Jun 30, 2020

Conversation

joe94
Copy link
Member

@joe94 joe94 commented Jun 8, 2020

No description provided.

@joe94 joe94 marked this pull request as draft June 8, 2020 23:58
@joe94 joe94 self-assigned this Jun 8, 2020
@joe94 joe94 linked an issue Jun 8, 2020 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Jun 9, 2020

Codecov Report

Merging #489 into master will decrease coverage by 0.04%.
The diff coverage is 90.47%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #489      +/-   ##
==========================================
- Coverage   86.76%   86.71%   -0.05%     
==========================================
  Files         186      186              
  Lines        8280     8272       -8     
==========================================
- Hits         7184     7173      -11     
- Misses        896      898       +2     
- Partials      200      201       +1     
Impacted Files Coverage Δ
device/mocks.go 37.50% <0.00%> (ø)
device/context.go 85.71% <66.66%> (ø)
device/manager.go 56.12% <66.66%> (-0.40%) ⬇️
device/device.go 66.03% <100.00%> (-3.78%) ⬇️
device/metadata.go 100.00% <100.00%> (+3.44%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 64a391c...40051ed. Read the comment docs.

@joe94 joe94 requested a review from johnabass June 10, 2020 19:33
m := make(Metadata)
m.SetJWTClaims(deepCopyMap(claims))
m.initSessionID()
func NewDeviceMetadataWithClaims(claims map[string]interface{}) *Metadata {
Copy link
Contributor

Choose a reason for hiding this comment

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

As a general design principle, you want to allow someone to do var m Metadata or m := new(Metadata) and have a Metadata object ready to go. That's preferable to a constructor. Sometimes you can't avoid having a constructor, but in this case you can if:

  • The internal Metadata methods should handle the case when the atomic.Value isn't initialized. This is easier than you might think, since a nil map is readable but not writable:
func (m *Metadata) loadData() map[string]interface{} {
    claims, _ := m.v.Load().(map[string]interface{})
    return claims // this can be nil for reads
}
  • Every exported method that sets keys or sets the claims wholesale should do the deep copy. Again, that's relatively easy and not very expensive if you're optimizing for reads.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can give that a try. I just found the constructors to be much simpler initializing defaults that should be available even on reads.

Yeah, we can change to a deepCopy of the entire map. It thought we might not need them since we're always overwriting only top keys.

@joe94 joe94 marked this pull request as ready for review June 12, 2020 00:37
@joe94 joe94 force-pushed the feature/metadata branch from d64d10e to 5070c1f Compare June 12, 2020 00:48
@joe94 joe94 requested a review from a team June 12, 2020 00:59
Copy link
Contributor

@johnabass johnabass left a comment

Choose a reason for hiding this comment

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

fantastic!

@joe94 joe94 merged commit fa6a57d into master Jun 30, 2020
@joe94 joe94 deleted the feature/metadata branch June 30, 2020 21:39
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.

Device metadata synchronization and slight optimization
2 participants