Skip to content

[Bug] avoid frequent lookup of the routing strategy env #956

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 9, 2025
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
4 changes: 4 additions & 0 deletions pkg/plugins/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/vllm-project/aibrix/pkg/cache"
routing "github.com/vllm-project/aibrix/pkg/plugins/gateway/algorithms"
"github.com/vllm-project/aibrix/pkg/utils"
)

func Test_ValidateRoutingStrategy(t *testing.T) {
Expand Down Expand Up @@ -118,6 +119,9 @@ func TestGetRoutingStrategy(t *testing.T) {
_ = os.Unsetenv("ROUTING_ALGORITHM")
}

// refresh default values, the process won't modify this environment variable during normal running
defaultRoutingStrategy, defaultRoutingStrategyEnabled = utils.LookupEnv(EnvRoutingAlgorithm)

routingStrategy, enabled := getRoutingStrategy(tt.headers)
assert.Equal(t, tt.expectedStrategy, routingStrategy, tt.message)
assert.Equal(t, tt.expectedEnabled, enabled, tt.message)
Expand Down
20 changes: 5 additions & 15 deletions pkg/plugins/gateway/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,20 @@ func validateStreamOptions(requestID string, user utils.User, jsonMap map[string
return nil
}

var defaultRoutingStrategy, defaultRoutingStrategyEnabled = utils.LookupEnv(EnvRoutingAlgorithm)

// getRoutingStrategy retrieves the routing strategy from the headers or environment variable
// It returns the routing strategy value and whether custom routing strategy is enabled.
func getRoutingStrategy(headers []*configPb.HeaderValue) (string, bool) {
var routingStrategy string
routingStrategyEnabled := false

// Check headers for routing strategy
for _, header := range headers {
if strings.ToLower(header.Key) == HeaderRoutingStrategy {
routingStrategy = string(header.RawValue)
routingStrategyEnabled = true
break // Prioritize header value over environment variable
}
}

// If header not set, check environment variable
if !routingStrategyEnabled {
if value, exists := utils.CheckEnvExists(EnvRoutingAlgorithm); exists {
routingStrategy = value
routingStrategyEnabled = true
return string(header.RawValue), true
}
}

return routingStrategy, routingStrategyEnabled
// If header not set, use default routing strategy from environment variable
return defaultRoutingStrategy, defaultRoutingStrategyEnabled
}

// getRequestMessage returns input request message field which has user prompt
Expand Down
6 changes: 3 additions & 3 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func TrimMessage(message string) string {
return message
}

// CheckEnvExists checks if an environment variable exists.
// LookupEnv retrieves an environment variable and returns whether it exists.
// It returns the value and a boolean indicating its existence.
func CheckEnvExists(envVar string) (string, bool) {
value, exists := os.LookupEnv(envVar)
func LookupEnv(key string) (string, bool) {
value, exists := os.LookupEnv(key)
return value, exists
}

Expand Down
Loading