-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support muti api keys and schedule strategy (#21)
- Loading branch information
Showing
9 changed files
with
68 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
OPENAI_API_KEY= | ||
LANGUAGE= | ||
LANGUAGE=en | ||
OPENAI_API_BASE_URL=api.openai.com | ||
API_KEY_STRATEGY=random | ||
LOCAL_PROXY= | ||
DISABLE_LOCAL_PROXY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const serverGlobalConfigs: { polling: number } = { | ||
// load balancer polling step | ||
polling: 0, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { serverGlobalConfigs } from '@configs/server'; | ||
import { StrategyMode } from '@interfaces'; | ||
|
||
export function loadBalancer<T>(arr: T[], strategy: StrategyMode = 'random') { | ||
if (!Array.isArray(arr) || arr.length === 0) return () => null; | ||
if (arr.length === 1) return () => arr[0]; | ||
|
||
if (strategy === 'polling') { | ||
// eslint-disable-next-line no-plusplus | ||
return () => arr[serverGlobalConfigs.polling++ % arr.length]; | ||
} | ||
|
||
return () => arr[Math.floor(Math.random() * arr.length)]; | ||
} |