Skip to content

Commit

Permalink
🔧 #164 Make client connection pool configurable across all providers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
daesu authored May 8, 2024
1 parent ab244b2 commit bd03442
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 23 deletions.
6 changes: 6 additions & 0 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,12 @@ const docTemplate = `{
"clients.ClientConfig": {
"type": "object",
"properties": {
"max_idle_connections": {
"type": "integer"
},
"max_idle_connections_per_host": {
"type": "integer"
},
"timeout": {
"type": "string"
}
Expand Down
6 changes: 6 additions & 0 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@
"clients.ClientConfig": {
"type": "object",
"properties": {
"max_idle_connections": {
"type": "integer"
},
"max_idle_connections_per_host": {
"type": "integer"
},
"timeout": {
"type": "string"
}
Expand Down
4 changes: 4 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ definitions:
type: object
clients.ClientConfig:
properties:
max_idle_connections:
type: integer
max_idle_connections_per_host:
type: integer
timeout:
type: string
type: object
Expand Down
5 changes: 2 additions & 3 deletions pkg/providers/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
// TODO: use values from the config
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
tel: tel,
Expand Down
5 changes: 2 additions & 3 deletions pkg/providers/azureopenai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
finishReasonMapper: openai.NewFinishReasonMapper(tel),
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
// TODO: use values from the config
Timeout: *clientConfig.Timeout,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
tel: tel,
Expand Down
5 changes: 2 additions & 3 deletions pkg/providers/bedrock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
// TODO: use values from the config
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
telemetry: tel,
Expand Down
10 changes: 8 additions & 2 deletions pkg/providers/clients/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ package clients
import "time"

type ClientConfig struct {
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout" swaggertype:"primitive,string"`
Timeout *time.Duration `yaml:"timeout,omitempty" json:"timeout" swaggertype:"primitive,string"`
MaxIdleConns *int `yaml:"max_idle_connections,omitempty" json:"max_idle_connections" swaggertype:"primitive,integer"`
MaxIdleConnsPerHost *int `yaml:"max_idle_connections_per_host,omitempty" json:"max_idle_connections_per_host" swaggertype:"primitive,integer"`
}

func DefaultClientConfig() *ClientConfig {
defaultTimeout := 10 * time.Second
maxIdleConns := 100
maxIdleConnsPerHost := 2

return &ClientConfig{
Timeout: &defaultTimeout,
Timeout: &defaultTimeout,
MaxIdleConns: &maxIdleConns,
MaxIdleConnsPerHost: &maxIdleConnsPerHost,
}
}
22 changes: 22 additions & 0 deletions pkg/providers/clients/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clients

import (
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -11,3 +12,24 @@ func TestClientConfig_DefaultConfig(t *testing.T) {

require.NotEmpty(t, config.Timeout)
}

func TestDefaultClientConfig(t *testing.T) {
config := DefaultClientConfig()

require.NotNil(t, config, "Config must not be nil")
require.NotNil(t, config.Timeout, "Timeout must not be nil")
require.NotNil(t, config.MaxIdleConns, "MaxIdleConns must not be nil")
require.NotNil(t, config.MaxIdleConnsPerHost, "MaxIdleConnsPerHost must not be nil")

// Check default timeout
expectedTimeout := 10 * time.Second
require.Equal(t, expectedTimeout, *config.Timeout)

// Check MaxIdleConns
expectedMaxIdleConns := 100
require.Equal(t, expectedMaxIdleConns, *config.MaxIdleConns)

// Check MaxIdleConnsPerHost
expectedMaxIdleConnsPerHost := 2
require.Equal(t, expectedMaxIdleConnsPerHost, *config.MaxIdleConnsPerHost)
}
5 changes: 2 additions & 3 deletions pkg/providers/cohere/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
// TODO: use values from the config
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
errMapper: NewErrorMapper(tel),
Expand Down
5 changes: 2 additions & 3 deletions pkg/providers/octoml/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
// TODO: use values from the config
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
telemetry: tel,
Expand Down
5 changes: 2 additions & 3 deletions pkg/providers/ollama/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
// TODO: use values from the config
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
telemetry: tel,
Expand Down
5 changes: 2 additions & 3 deletions pkg/providers/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
// TODO: use values from the config
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 2,
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
},
},
tel: tel,
Expand Down

0 comments on commit bd03442

Please sign in to comment.