Skip to content

Commit

Permalink
Merge branch 'main' into docs-ctx-locals
Browse files Browse the repository at this point in the history
  • Loading branch information
sixcolors authored Jun 17, 2024
2 parents 6743f96 + d19b893 commit 7e83239
Show file tree
Hide file tree
Showing 3 changed files with 12 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
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
14 changes: 5 additions & 9 deletions middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cache

import (
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -95,22 +96,17 @@ func New(config ...Config) fiber.Handler {
return c.Next()
}

// Only cache selected methods
var isExists bool
for _, method := range cfg.Methods {
if c.Method() == method {
isExists = true
}
}
requestMethod := c.Method()

if !isExists {
// Only cache selected methods
if !slices.Contains(cfg.Methods, requestMethod) {
c.Set(cfg.CacheHeader, cacheUnreachable)
return c.Next()
}

// Get key from request
// TODO(allocation optimization): try to minimize the allocation from 2 to 1
key := cfg.KeyGenerator(c) + "_" + c.Method()
key := cfg.KeyGenerator(c) + "_" + requestMethod

// Get entry from pool
e := manager.get(key)
Expand Down

0 comments on commit 7e83239

Please sign in to comment.