Skip to content
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: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ jobs:
- name: Build
working-directory: web
run: |
npm install -g pnpm
pnpm install && pnpm run build
corepack pnpm install --frozen-lockfile
corepack pnpm run build

- name: Upload Artifact
uses: actions/upload-artifact@v7
Expand Down
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ WORKDIR /aiproxy/web

COPY ./web/ ./

RUN npm install -g pnpm

RUN pnpm install && pnpm run build
RUN corepack pnpm install --frozen-lockfile && corepack pnpm run build

FROM golang:1.26-alpine AS builder

Expand Down
73 changes: 27 additions & 46 deletions core/controller/relay-channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,6 @@ func getChannelErrorRate(errorRates map[int64]float64, channelID int64) float64
return errorRates[channelID]
}

func pickMinErrorRateHasPermissionChannel(
current *model.Channel,
currentErrorRate float64,
candidate *model.Channel,
candidateErrorRate float64,
) *model.Channel {
if candidate == nil {
return current
}

if current == nil {
return candidate
}

if candidateErrorRate < currentErrorRate {
return candidate
}

return current
}

func pickChannel(
channels []*model.Channel,
errorRates map[int64]float64,
Expand Down Expand Up @@ -656,28 +635,21 @@ func getRetryChannel(
}
}

if state.exhausted {
if state.lastMinErrorRateHasPermissionChannel == nil {
if state.designatedChannel != nil {
// Explicitly selected channels stay pinned for the request.
channelID := int64(state.designatedChannel.ID)
if _, ignored := state.ignoreChannelIDs[channelID]; ignored {
return nil, ErrChannelsExhausted
}

// Check if the lowest-error has-permission channel has high error rate.
// If so, return exhausted to prevent retrying with a bad channel
channelID := int64(state.lastMinErrorRateHasPermissionChannel.ID)
if errorRate := getChannelErrorRate(errorRates, channelID); errorRate > maxRetryErrorRate {
return nil, ErrChannelsExhausted
}

return state.lastMinErrorRateHasPermissionChannel, nil
return state.designatedChannel, nil
}

filteredChannels := filterChannels(
state.migratedChannels,
errorRates,
maxRetryErrorRate,
state.ignoreChannelIDs,
state.failedChannelIDs,
)
filteredChannels := getRetryCandidates(state, errorRates)

if len(state.preferChannelIDs) > 0 {
newChannel := pickPreferredChannel(
Expand All @@ -694,27 +666,36 @@ func getRetryChannel(
errorRates,
)
if err != nil {
if !errors.Is(err, ErrChannelsExhausted) ||
state.lastMinErrorRateHasPermissionChannel == nil {
if !errors.Is(err, ErrChannelsExhausted) || len(state.failedChannelIDs) == 0 {
return nil, err
}

// Check if the lowest-error has-permission channel has high error rate.
// If so, return exhausted to prevent retrying with a bad channel
channelID := int64(state.lastMinErrorRateHasPermissionChannel.ID)
if errorRate := getChannelErrorRate(errorRates, channelID); errorRate > maxRetryErrorRate {
return nil, ErrChannelsExhausted
}

// Check if the lowest-error has-permission channel is still healthy before using it.
state.exhausted = true
// Start a new round so every currently eligible channel gets another attempt.
state.failedChannelIDs = make(map[int64]struct{})
state.preferChannelIDs = nil

return state.lastMinErrorRateHasPermissionChannel, nil
return pickChannel(
getRetryCandidates(state, errorRates),
errorRates,
)
}

return newChannel, nil
}

func getRetryCandidates(
state *retryState,
errorRates map[int64]float64,
) []*model.Channel {
return filterChannels(
state.migratedChannels,
errorRates,
maxRetryErrorRate,
state.ignoreChannelIDs,
state.failedChannelIDs,
)
}

func filterChannels(
channels []*model.Channel,
errorRates map[int64]float64,
Expand Down
Loading
Loading