Skip to content

🔧 #186: Rendering Durations in a human-friendly way #202

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

Merged
merged 1 commit into from
Apr 11, 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
10 changes: 10 additions & 0 deletions pkg/config/fields/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package fields

import "time"

type Duration time.Duration

// MarshalText serializes Durations in a human-friendly way (it's shown in nanoseconds by default)
func (d Duration) MarshalText() ([]byte, error) {
return []byte(time.Duration(d).String()), nil
}
6 changes: 4 additions & 2 deletions pkg/providers/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"time"

"glide/pkg/config/fields"

"glide/pkg/routers/health"

"glide/pkg/api/schemas"
Expand Down Expand Up @@ -40,7 +42,7 @@ type LanguageModel struct {
healthTracker *health.Tracker
chatLatency *latency.MovingAverage
chatStreamLatency *latency.MovingAverage
latencyUpdateInterval *time.Duration
latencyUpdateInterval *fields.Duration
}

func NewLangModel(modelID string, client LangProvider, budget *health.ErrorBudget, latencyConfig latency.Config, weight int) *LanguageModel {
Expand All @@ -67,7 +69,7 @@ func (m LanguageModel) Weight() int {
return m.weight
}

func (m LanguageModel) LatencyUpdateInterval() *time.Duration {
func (m LanguageModel) LatencyUpdateInterval() *fields.Duration {
return m.latencyUpdateInterval
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/providers/provider.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package providers

import (
"time"
"glide/pkg/config/fields"
)

// ModelProvider exposes provider context
Expand All @@ -13,6 +13,6 @@ type ModelProvider interface {
type Model interface {
ID() string
Healthy() bool
LatencyUpdateInterval() *time.Duration
LatencyUpdateInterval() *fields.Duration
Weight() int
}
6 changes: 4 additions & 2 deletions pkg/providers/testing/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package testing
import (
"time"

"glide/pkg/config/fields"

"glide/pkg/providers"
"glide/pkg/routers/latency"
)
Expand Down Expand Up @@ -42,10 +44,10 @@ func (m *LangModelMock) ChatLatency() *latency.MovingAverage {
return m.chatLatency
}

func (m LangModelMock) LatencyUpdateInterval() *time.Duration {
func (m LangModelMock) LatencyUpdateInterval() *fields.Duration {
updateInterval := 30 * time.Second

return &updateInterval
return (*fields.Duration)(&updateInterval)
}

func (m LangModelMock) Weight() int {
Expand Down
14 changes: 9 additions & 5 deletions pkg/routers/latency/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package latency

import "time"
import (
"time"

"glide/pkg/config/fields"
)

// Config defines setting for moving average latency calculations
type Config struct {
Decay float64 `yaml:"decay" json:"decay"` // Weight of new latency measurements
WarmupSamples uint8 `yaml:"warmup_samples" json:"warmup_samples"` // The number of latency probes required to init moving average
UpdateInterval *time.Duration `yaml:"update_interval,omitempty" json:"update_interval" swaggertype:"primitive,string"` // How often gateway should probe models with not the lowest response latency
Decay float64 `yaml:"decay" json:"decay"` // Weight of new latency measurements
WarmupSamples uint8 `yaml:"warmup_samples" json:"warmup_samples"` // The number of latency probes required to init moving average
UpdateInterval *fields.Duration `yaml:"update_interval,omitempty" json:"update_interval" swaggertype:"primitive,string"` // How often gateway should probe models with not the lowest response latency
}

func DefaultConfig() *Config {
Expand All @@ -15,6 +19,6 @@ func DefaultConfig() *Config {
return &Config{
Decay: 0.06,
WarmupSamples: 3,
UpdateInterval: &defaultUpdateInterval,
UpdateInterval: (*fields.Duration)(&defaultUpdateInterval),
}
}
2 changes: 1 addition & 1 deletion pkg/routers/routing/least_latency.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *ModelSchedule) Update() {
s.mu.Lock()
defer s.mu.Unlock()

s.expireAt = time.Now().Add(*s.model.LatencyUpdateInterval())
s.expireAt = time.Now().Add(time.Duration(*s.model.LatencyUpdateInterval()))
}

// LeastLatencyRouting routes requests to the model that responses the fastest
Expand Down