Skip to content
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

feat: add global concurrency limit #228

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export interface Config extends PromptConfig, ParamConfig {
requestTimeout?: number
recallTimeout?: number
maxConcurrency?: number
globalConcurrency?: number
pollInterval?: number
trustedWorkers?: boolean
}
Expand Down Expand Up @@ -389,6 +390,7 @@ export const Config = Schema.intersect([
requestTimeout: Schema.number().role('time').description('当请求超过这个时间时会中止并提示超时。').default(Time.minute),
recallTimeout: Schema.number().role('time').description('图片发送后自动撤回的时间 (设置为 0 以禁用此功能)。').default(0),
maxConcurrency: Schema.number().description('单个频道下的最大并发数量 (设置为 0 以禁用此功能)。').default(0),
globalConcurrency: Schema.number().min(0).description('全局的最大并发数量 (设置为 0 以禁用此功能)。').default(0),
}).description('高级设置'),
]) as Schema<Config>

Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Computed, Context, Dict, h, Logger, omit, Quester, Session, SessionError, trimSlash } from 'koishi'
import { Computed, Context, Dict, h, Logger, omit, Quester, Session, SessionError, sleep, trimSlash } from 'koishi'
import { Config, modelMap, models, orientMap, parseInput, sampler, upscalers, scheduler } from './config'
import { ImageData, StableDiffusionWebUI } from './types'
import { closestMultiple, download, forceDataPrefix, getImageSize, login, NetworkError, project, resizeInput, Size } from './utils'
Expand Down Expand Up @@ -285,6 +285,12 @@
session.send(globalTasks.size
? session.text('.pending', [globalTasks.size])
: session.text('.waiting'))

if (config.globalConcurrency) {
while (globalTasks.size >= config.globalConcurrency) {
await sleep(100)
}
}
MaikoTan marked this conversation as resolved.
Show resolved Hide resolved

container.forEach((id) => globalTasks.add(id))
const cleanUp = (id: string) => {
Expand Down Expand Up @@ -428,7 +434,6 @@
const uuid = res.data.id

const check = () => ctx.http.get(trimSlash(config.endpoint) + '/api/v2/generate/check/' + uuid).then((res) => res.done)
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
while (await check() === false) {
await sleep(config.pollInterval)
}
Expand Down Expand Up @@ -515,7 +520,7 @@

const messageIds = await session.send(getContent())
if (messageIds.length && config.recallTimeout) {
ctx.setTimeout(() => {

Check failure on line 523 in src/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'setTimeout' does not exist on type 'Context'.

Check failure on line 523 in src/index.ts

View workflow job for this annotation

GitHub Actions / build

Property 'setTimeout' does not exist on type 'Context'.
for (const id of messageIds) {
session.bot.deleteMessage(session.channelId, id)
}
Expand Down
Loading