Skip to content

Commit

Permalink
🔥 feat: Add support for creating Fiber client from existing FastHTTP …
Browse files Browse the repository at this point in the history
…client (#3214)

* add support to create client from existing client

* add NewWithClient to documentation

* fix typo in comment

* fix and shorten comment

* add unit test for NewWithClient

* add nil check and test

* fix lint check
  • Loading branch information
mitulagr2 authored Nov 25, 2024
1 parent f08ebf4 commit 3593436
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,16 @@ func New() *Client {
// trie to use a pool to reduce the cost of memory allocation
// for the fiber client and the fasthttp client
// if possible also for other structs -> request header, cookie, query param, path param...
return NewWithClient(&fasthttp.Client{})
}

// NewWithClient creates and returns a new Client object from an existing client.
func NewWithClient(c *fasthttp.Client) *Client {
if c == nil {
panic("fasthttp.Client must not be nil")
}
return &Client{
fasthttp: &fasthttp.Client{},
fasthttp: c,
header: &Header{
RequestHeader: &fasthttp.RequestHeader{},
},
Expand Down
23 changes: 23 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,29 @@ func startTestServerWithPort(t *testing.T, beforeStarting func(app *fiber.App))
return nil, ""
}

func Test_New_With_Client(t *testing.T) {
t.Parallel()

t.Run("with valid client", func(t *testing.T) {
t.Parallel()

c := &fasthttp.Client{
MaxConnsPerHost: 5,
}
client := NewWithClient(c)

require.NotNil(t, client)
})

t.Run("with nil client", func(t *testing.T) {
t.Parallel()

require.PanicsWithValue(t, "fasthttp.Client must not be nil", func() {
NewWithClient(nil)
})
})
}

func Test_Client_Add_Hook(t *testing.T) {
t.Parallel()

Expand Down
10 changes: 9 additions & 1 deletion docs/client/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,22 @@ type Client struct {
}
```

New
### New

New creates and returns a new Client object.

```go title="Signature"
func New() *Client
```

### NewWithClient

NewWithClient creates and returns a new Client object from an existing client object.

```go title="Signature"
func NewWithClient(c *fasthttp.Client) *Client
```

## REST Methods

### Get
Expand Down

1 comment on commit 3593436

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 3593436 Previous: 8c84b0f Ratio
Benchmark_Ctx_Send 6.54 ns/op 0 B/op 0 allocs/op 4.335 ns/op 0 B/op 0 allocs/op 1.51
Benchmark_Ctx_Send - ns/op 6.54 ns/op 4.335 ns/op 1.51
Benchmark_Utils_GetOffer/1_parameter 205.9 ns/op 0 B/op 0 allocs/op 131 ns/op 0 B/op 0 allocs/op 1.57
Benchmark_Utils_GetOffer/1_parameter - ns/op 205.9 ns/op 131 ns/op 1.57
Benchmark_Middleware_BasicAuth - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_Middleware_BasicAuth_Upper - B/op 80 B/op 48 B/op 1.67
Benchmark_Middleware_BasicAuth_Upper - allocs/op 5 allocs/op 3 allocs/op 1.67
Benchmark_CORS_NewHandler - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandler - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - B/op 16 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerSingleOrigin - allocs/op 1 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflight - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflight - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightSingleOrigin - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - B/op 104 B/op 0 B/op +∞
Benchmark_CORS_NewHandlerPreflightWildcard - allocs/op 5 allocs/op 0 allocs/op +∞
Benchmark_Middleware_CSRF_GenerateToken - B/op 513 B/op 327 B/op 1.57
Benchmark_Middleware_CSRF_GenerateToken - allocs/op 10 allocs/op 6 allocs/op 1.67

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.