-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Global DefaultOptions #4
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #4 +/- ##
========================================
Coverage ? 100.00%
========================================
Files ? 1
Lines ? 146
Branches ? 0
========================================
Hits ? 146
Misses ? 0
Partials ? 0 ☔ View full report in Codecov by Sentry. |
Leaving the pull request open for feedback for a while before merging! |
`cookie.DefaultOptions` variable: | ||
|
||
```go | ||
cookie.DefaultOptions = &cookie.Options{ |
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'm a bit afraid by such behavior. It could lead to race conditions if your lib is used by multiple libs.
I would recommend a pattern that will be immutable
Something like
mySignedCookieHelper := cookie.WithOptions( /* */)
mySignedCookieHelper.SetCookie("foo", "bar")
Also without such a thing, tests could become a nightmare as you are accessing sane global variable.
Otherwise, you will need to use mutex lock/unlock.
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.
What do you think about this?
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.
How would this lead to race conditions?
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.
cookie.WithOptions would return a soped variable and no global variable will be used.
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.
Sure, but that's an entire refactor of the package to introduce instanced cookie managers rather than a global one...
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'm not necessarily entirely against this, but it's a pretty massive change in direction.
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 agree. It's put of the scope of your change. But, unless if I'm wrong. Before this PR there was no global variable that could cause race condition.
I think it's a needed refactoring, but yes, it requires to change a lot of things.
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 encourage you to check out #8 as I'm currently exploring what this might look like. I think the ideal of having a DefaultManager
available so package-level functions continue to work is ideal, but all of this is why I'm still v0 of my package 😆
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 do appreciate.
For records, I'm not even sure current implementation, so not what you are doing with #8, would have passed tests if you used go test -race ./...
The intent of this is to enable you to globally set
DefaultOptions
so that it's not strictly necessary to set them across usages ofcookie.Set
Additionally, this should allow for use of
signed
to be true by default.TODO
PopulateFromCookies
to be able to understandunsigned
as well when the default value is signed.net/http
packageOpen to feedback during early stages of this PR.