Skip to content

Commit

Permalink
fix Bug: novelai队列429错误 #217
Browse files Browse the repository at this point in the history
  • Loading branch information
MirrorCY committed Feb 8, 2024
1 parent dcfda09 commit b6498fc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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 @@ export function apply(ctx: Context, config: Config) {
session.send(globalTasks.size
? session.text('.pending', [globalTasks.size])
: session.text('.waiting'))

if (config.globalConcurrency) {
while (globalTasks.size >= config.globalConcurrency) {
await sleep(100)
}
}

container.forEach((id) => globalTasks.add(id))
const cleanUp = (id: string) => {
Expand Down Expand Up @@ -428,7 +434,6 @@ export function apply(ctx: Context, config: Config) {
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

0 comments on commit b6498fc

Please sign in to comment.