Skip to content

Commit

Permalink
Merge branch 'main' into cache-invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
hcancelik authored Jun 18, 2024
2 parents 9a6d0a8 + 011e83b commit d56e1f9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

- name: Upload coverage reports to Codecov
if: ${{ matrix.platform == 'ubuntu-latest' && matrix.go-version == '1.22.x' }}
uses: codecov/codecov-action@v4.4.1
uses: codecov/codecov-action@v4.5.0
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.txt
Expand Down
11 changes: 10 additions & 1 deletion ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@ func (c *DefaultCtx) Links(link ...string) {

// Locals makes it possible to pass any values under keys scoped to the request
// and therefore available to all following routes that match the request.
//
// All the values are removed from ctx after returning from the top
// RequestHandler. Additionally, Close method is called on each value
// implementing io.Closer before removing the value from ctx.
func (c *DefaultCtx) Locals(key any, value ...any) any {
if len(value) == 0 {
return c.fasthttp.UserValue(key)
Expand All @@ -868,7 +872,12 @@ func (c *DefaultCtx) Locals(key any, value ...any) any {
}

// Locals function utilizing Go's generics feature.
// This function allows for manipulating and retrieving local values within a request context with a more specific data type.
// This function allows for manipulating and retrieving local values within a
// request context with a more specific data type.
//
// All the values are removed from ctx after returning from the top
// RequestHandler. Additionally, Close method is called on each value
// implementing io.Closer before removing the value from ctx.
func Locals[V any](c Ctx, key any, value ...V) V {
var v V
var ok bool
Expand Down
4 changes: 4 additions & 0 deletions ctx_interface_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions docs/api/ctx.md
Original file line number Diff line number Diff line change
Expand Up @@ -945,10 +945,10 @@ app.Get("/", func(c fiber.Ctx) error {
## Locals
A method that stores variables scoped to the request and, therefore, are available only to the routes that match the request.
A method that stores variables scoped to the request and, therefore, are available only to the routes that match the request. The stored variables are removed after the request is handled. If any of the stored data implements the `io.Closer` interface, its `Close` method will be called before it's removed.
:::tip
This is useful if you want to pass some **specific** data to the next middleware.
This is useful if you want to pass some **specific** data to the next middleware. Remember to perform type assertions when retrieving the data to ensure it is of the expected type. You can also use a non-exported type as a key to avoid collisions.
:::
```go title="Signature"
Expand All @@ -957,26 +957,26 @@ func (c Ctx) Locals(key any, value ...any) any
```go title="Example"

// key is an unexported type for keys defined in this package.
// keyType is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int
type keyType int

// userKey is the key for user.User values in Contexts. It is
// unexported; clients use user.NewContext and user.FromContext
// instead of using this key directly.
var userKey key
var userKey keyType

app.Use(func(c fiber.Ctx) error {
c.Locals(userKey, "admin")
c.Locals(userKey, "admin") // Stores the string "admin" under a non-exported type key
return c.Next()
})

app.Get("/admin", func(c fiber.Ctx) error {
if c.Locals(userKey) == "admin" {
user, ok := c.Locals(userKey).(string) // Retrieves the data stored under the key and performs a type assertion
if ok && user == "admin" {
return c.Status(fiber.StatusOK).SendString("Welcome, admin!")
}
return c.SendStatus(fiber.StatusForbidden)

})
```
Expand Down
9 changes: 6 additions & 3 deletions docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ go get github.com/gofiber/fiber/v3
```

### Zero Allocation
Some values returned from **fiber.Ctx** are **not** immutable by default.
Because fiber is optimized for **high-performance** care is needed when using **fiber.Ctx**:

Because fiber is optimized for **high-performance**, values returned from **fiber.Ctx** are **not** immutable by default and **will** be re-used across requests. As a rule of thumb, you **must** only use context values within the handler, and you **must not** keep any references. As soon as you return from the handler, any values you have obtained from the context will be re-used in future requests and will change below your feet. Here is an example:
- Some values returned from **fiber.Ctx** are **not** immutable by default, and **will** be re-used across requests.
- **fiber.Ctx** is **not** thread-safe and **must not** be accessed concurrently by multiple goroutines, the immutable setting **does not** change this.

As a rule of thumb, you **must** only use context values within the handler, and you **must not** keep any references. As soon as you return from the handler, any values you have obtained from the context will be re-used in future requests and will change below your feet. Here is an example:

```go
func handler(c fiber.Ctx) error {
Expand Down Expand Up @@ -70,7 +73,7 @@ app := fiber.New(fiber.Config{
})
```

For more information, please check [**\#426**](https://github.com/gofiber/fiber/issues/426) and [**\#185**](https://github.com/gofiber/fiber/issues/185).
For more information, please check [**\#426**](https://github.com/gofiber/fiber/issues/426), [**\#185**](https://github.com/gofiber/fiber/issues/185) and [**\#3012**](https://github.com/gofiber/fiber/issues/3012).

### Hello, World!

Expand Down

0 comments on commit d56e1f9

Please sign in to comment.