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

🔧 #238 Implements human readable durations in config #253

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion pkg/providers/anthropic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"time"

"github.com/EinStack/glide/pkg/telemetry"

Expand Down Expand Up @@ -46,7 +47,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/azureopenai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/http"
"time"

"github.com/EinStack/glide/pkg/providers/openai"

Expand Down Expand Up @@ -50,7 +51,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
finishReasonMapper: openai.NewFinishReasonMapper(tel),
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/bedrock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"net/url"
"time"

"github.com/EinStack/glide/pkg/telemetry"

Expand Down Expand Up @@ -56,7 +57,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
config: providerConfig,
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
14 changes: 9 additions & 5 deletions pkg/providers/clients/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package clients

import "time"
import (
"time"

"github.com/EinStack/glide/pkg/config/fields"
)

type ClientConfig struct {
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"`
Timeout *fields.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 {
Expand All @@ -14,7 +18,7 @@ func DefaultClientConfig() *ClientConfig {
maxIdleConnsPerHost := 2

return &ClientConfig{
Timeout: &defaultTimeout,
Timeout: (*fields.Duration)(&defaultTimeout),
MaxIdleConns: &maxIdleConns,
MaxIdleConnsPerHost: &maxIdleConnsPerHost,
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/providers/clients/config_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package clients

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/EinStack/glide/pkg/config/fields"
)

func TestClientConfig_DefaultConfig(t *testing.T) {
Expand All @@ -13,6 +16,21 @@ func TestClientConfig_DefaultConfig(t *testing.T) {
require.NotEmpty(t, config.Timeout)
}

func TestClientConfig_JSONMarshal(t *testing.T) {
defaultConfig := DefaultClientConfig()

expectedJSON := `{
"timeout": "10s",
"max_idle_connections": 100,
"max_idle_connections_per_host": 2
}`

marshaledJSON, err := json.MarshalIndent(defaultConfig, "", "\t")

require.NoError(t, err)
require.JSONEq(t, expectedJSON, string(marshaledJSON))
}

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

Expand All @@ -22,7 +40,7 @@ func TestDefaultClientConfig(t *testing.T) {
require.NotNil(t, config.MaxIdleConnsPerHost, "MaxIdleConnsPerHost must not be nil")

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

// Check MaxIdleConns
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/cohere/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"time"

"github.com/EinStack/glide/pkg/telemetry"

Expand Down Expand Up @@ -44,7 +45,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
config: providerConfig,
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/octoml/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"time"

"github.com/EinStack/glide/pkg/telemetry"

Expand Down Expand Up @@ -44,7 +45,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/ollama/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"time"

"github.com/EinStack/glide/pkg/telemetry"

Expand Down Expand Up @@ -42,7 +43,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
config: providerConfig,
chatRequestTemplate: NewChatRequestFromConfig(providerConfig),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/openai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"net/url"
"time"

"go.uber.org/zap"

Expand Down Expand Up @@ -53,7 +54,7 @@ func NewClient(providerConfig *Config, clientConfig *clients.ClientConfig, tel *
finishReasonMapper: NewFinishReasonMapper(tel),
errMapper: NewErrorMapper(tel),
httpClient: &http.Client{
Timeout: *clientConfig.Timeout,
Timeout: time.Duration(*clientConfig.Timeout),
Transport: &http.Transport{
MaxIdleConns: *clientConfig.MaxIdleConns,
MaxIdleConnsPerHost: *clientConfig.MaxIdleConnsPerHost,
Expand Down
6 changes: 4 additions & 2 deletions pkg/routers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package routers

import (
"fmt"
"time"

"github.com/EinStack/glide/pkg/telemetry"

Expand Down Expand Up @@ -164,12 +165,13 @@ func (c *LangRouterConfig) BuildModels(tel *telemetry.Telemetry) ([]*providers.L

func (c *LangRouterConfig) BuildRetry() *retry.ExpRetry {
retryConfig := c.Retry
maxDelay := time.Duration(*retryConfig.MaxDelay)

return retry.NewExpRetry(
retryConfig.MaxRetries,
retryConfig.BaseMultiplier,
retryConfig.MinDelay,
retryConfig.MaxDelay,
time.Duration(retryConfig.MinDelay),
&maxDelay,
)
}

Expand Down
18 changes: 11 additions & 7 deletions pkg/routers/retry/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package retry

import "time"
import (
"time"

"github.com/EinStack/glide/pkg/config/fields"
)

type ExpRetryConfig struct {
MaxRetries int `yaml:"max_retries,omitempty" json:"max_retries"`
BaseMultiplier int `yaml:"base_multiplier,omitempty" json:"base_multiplier"`
MinDelay time.Duration `yaml:"min_delay,omitempty" json:"min_delay" swaggertype:"primitive,integer"`
MaxDelay *time.Duration `yaml:"max_delay,omitempty" json:"max_delay" swaggertype:"primitive,integer"`
MaxRetries int `yaml:"max_retries,omitempty" json:"max_retries"`
BaseMultiplier int `yaml:"base_multiplier,omitempty" json:"base_multiplier"`
MinDelay fields.Duration `yaml:"min_delay,omitempty" json:"min_delay" swaggertype:"primitive,string"`
MaxDelay *fields.Duration `yaml:"max_delay,omitempty" json:"max_delay" swaggertype:"primitive,string"`
}

func DefaultExpRetryConfig() *ExpRetryConfig {
maxDelay := 5 * time.Second
maxDelay := fields.Duration(5 * time.Second)

return &ExpRetryConfig{
MaxRetries: 3,
BaseMultiplier: 2,
MinDelay: 2 * time.Second,
MinDelay: fields.Duration(2 * time.Second),
MaxDelay: &maxDelay,
}
}
16 changes: 16 additions & 0 deletions pkg/routers/retry/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package retry

import (
"encoding/json"
"testing"

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

require.NotNil(t, config)
}

func TestRetryConfig_JSONMarshal(t *testing.T) {
defaultConfig := DefaultExpRetryConfig()

expectedJSON := `{
"max_retries": 3,
"base_multiplier": 2,
"min_delay": "2s",
"max_delay": "5s"
}`

marshaledJSON, err := json.MarshalIndent(defaultConfig, "", "\t")
require.NoError(t, err)
require.JSONEq(t, expectedJSON, string(marshaledJSON))
}
Loading