-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
♻️ v3: fix!: ContextKey collisions #2781
♻️ v3: fix!: ContextKey collisions #2781
Conversation
fixed to contrib packages: gofiber/contrib#896 |
@benjajaja tag |
@ReneWerner87 I didn't see a v3 branch in the repo, please let me know how you want to handle this for contrib. |
we haven't created one there yet, but you are welcome to create a beta branch |
with this concept it is no longer possible to customize the context key from the outside does this have any disadvantages for us? @gofiber/maintainers @gofiber/contributors otherwise the code looks well implemented according to this concept |
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.
Awesome, you did a good job!
I am happy with the change since it's recommended behavior by Go context docs. |
Agree, with the statement |
I didn’t envision anyone using the same middleware twice with the same routes. In my opinion the package functions improve the developer experience and make the framework more user-friendly. Also some existing middleware already use this approach with predicate functions. Is there is a compelling use case for changing the approach or some documentation changes requested? |
Currently this should work, just wanted to mention it |
A pretty minor point here, but you could use |
Where there are multiple keys it would wind up something like this: type (
contextKeyUsername struct{}
contextKeyPassword struct{}
)
var (
usernameKey contextKeyUsername
passwordKey contextKeyPassword
) In the above code, two key variables are stored on the stack, and they point to an instance of contextKey*. Because contextKey* is an empty struct, it does not take up any memory, and there are no heap allocations. However, the variable keys take up a small amount of stack space (the size of a pointer). vs type contextKey int
const (
usernameKey contextKey = iota
passwordKey
) This approach does not cause any heap allocations or take up any runtime stack space because constants in Go are replaced by their values at compile time where they are used. So, AFAIK both approaches would ensure type safety and uniqueness of keys. And have zero allocs on the heap. I preferred the style of iota when multiple context keys were used, also stand lib context package uses a However, what does everyone else prefer? Do you find the distinct struct types or iota style more readable and maintainable? |
Since the allocations are at the registration time it doesn't matter for me But i prefer the iota variant |
From what I understand, in Go, constants are not allocated in memory like variables are. Instead, they are replaced by their values at compile time and do not have an address. This implies that constants do not exist while the program is running and there is no memory allocation for them other than the executable program code. Therefore, using const with iota would appear to be a zero allocation solution. However, please let me know if I've misunderstood anything. |
Description
Fixes context key collision issues in middleware and
ctx.go
. Follows best practice/convention from context by using unexported key types to avoid collisions.Related to #2684 which outlines the issue and rationale for change.
Changes Introduced
List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.
Type of Change
Please delete options that are not relevant.
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/
directory for Fiber's documentation.Commit Formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md