Skip to content

Commit b2b9ac6

Browse files
authored
[Bug] avoid frequent lookup of the routing strategy env (vllm-project#956)
Signed-off-by: Iceber Gu <[email protected]>
1 parent 71cc6ae commit b2b9ac6

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

pkg/plugins/gateway/gateway_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/stretchr/testify/assert"
2525
"github.com/vllm-project/aibrix/pkg/cache"
2626
routing "github.com/vllm-project/aibrix/pkg/plugins/gateway/algorithms"
27+
"github.com/vllm-project/aibrix/pkg/utils"
2728
)
2829

2930
func Test_ValidateRoutingStrategy(t *testing.T) {
@@ -118,6 +119,9 @@ func TestGetRoutingStrategy(t *testing.T) {
118119
_ = os.Unsetenv("ROUTING_ALGORITHM")
119120
}
120121

122+
// refresh default values, the process won't modify this environment variable during normal running
123+
defaultRoutingStrategy, defaultRoutingStrategyEnabled = utils.LookupEnv(EnvRoutingAlgorithm)
124+
121125
routingStrategy, enabled := getRoutingStrategy(tt.headers)
122126
assert.Equal(t, tt.expectedStrategy, routingStrategy, tt.message)
123127
assert.Equal(t, tt.expectedEnabled, enabled, tt.message)

pkg/plugins/gateway/util.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,20 @@ func validateStreamOptions(requestID string, user utils.User, jsonMap map[string
5050
return nil
5151
}
5252

53+
var defaultRoutingStrategy, defaultRoutingStrategyEnabled = utils.LookupEnv(EnvRoutingAlgorithm)
54+
5355
// getRoutingStrategy retrieves the routing strategy from the headers or environment variable
5456
// It returns the routing strategy value and whether custom routing strategy is enabled.
5557
func getRoutingStrategy(headers []*configPb.HeaderValue) (string, bool) {
56-
var routingStrategy string
57-
routingStrategyEnabled := false
58-
5958
// Check headers for routing strategy
6059
for _, header := range headers {
6160
if strings.ToLower(header.Key) == HeaderRoutingStrategy {
62-
routingStrategy = string(header.RawValue)
63-
routingStrategyEnabled = true
64-
break // Prioritize header value over environment variable
65-
}
66-
}
67-
68-
// If header not set, check environment variable
69-
if !routingStrategyEnabled {
70-
if value, exists := utils.CheckEnvExists(EnvRoutingAlgorithm); exists {
71-
routingStrategy = value
72-
routingStrategyEnabled = true
61+
return string(header.RawValue), true
7362
}
7463
}
7564

76-
return routingStrategy, routingStrategyEnabled
65+
// If header not set, use default routing strategy from environment variable
66+
return defaultRoutingStrategy, defaultRoutingStrategyEnabled
7767
}
7868

7969
// getRequestMessage returns input request message field which has user prompt

pkg/utils/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ func TrimMessage(message string) string {
7474
return message
7575
}
7676

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

0 commit comments

Comments
 (0)