Skip to content
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

🔥 feat: Add support for creating Fiber client from existing FastHTTP client #3214

Merged
merged 7 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
gaby marked this conversation as resolved.
Show resolved Hide resolved
mitulagr2 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading