Add USE_WORKER_THREADS environment var
This commit is contained in:
parent
15b69c510c
commit
9ee76b10fa
@ -1,10 +1,5 @@
|
|||||||
import { gql } from "../mods.js";
|
import { gql } from "../mods.js";
|
||||||
import {
|
import { Sudoku, GenerateArguments, SolveArguments } from "../sudoku/index.js";
|
||||||
solve,
|
|
||||||
generate,
|
|
||||||
GenerateArguments,
|
|
||||||
SolveArguments,
|
|
||||||
} from "../sudoku/index.js";
|
|
||||||
|
|
||||||
export const typeDefs = gql`
|
export const typeDefs = gql`
|
||||||
"""
|
"""
|
||||||
@ -33,9 +28,16 @@ export const typeDefs = gql`
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
interface SudokuFuncs {
|
||||||
|
solve(args: SolveArguments): Promise<Sudoku> | Sudoku;
|
||||||
|
generate(args: GenerateArguments): Promise<Sudoku> | Sudoku;
|
||||||
|
}
|
||||||
|
|
||||||
export const resolvers = {
|
export const resolvers = {
|
||||||
Query: {
|
Query: {
|
||||||
generate: (obj: any, args: GenerateArguments) => generate(args),
|
generate: (obj: any, args: GenerateArguments, ctx: SudokuFuncs) =>
|
||||||
solve: (obj: any, args: SolveArguments) => solve(args),
|
ctx.generate(args),
|
||||||
|
solve: (obj: any, args: SolveArguments, ctx: SudokuFuncs) =>
|
||||||
|
ctx.solve(args),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
22
src/main.ts
22
src/main.ts
@ -1,6 +1,13 @@
|
|||||||
import { Application, bodyParser } from "./mods.js";
|
import { Application, bodyParser } from "./mods.js";
|
||||||
import { applyGraphQL } from "./graphql.js";
|
import { applyGraphQL } from "./graphql.js";
|
||||||
import { typeDefs, resolvers } from "./graphql/index.js";
|
import { typeDefs, resolvers } from "./graphql/index.js";
|
||||||
|
import {
|
||||||
|
initializeWorkers,
|
||||||
|
solve,
|
||||||
|
generate,
|
||||||
|
solveSync,
|
||||||
|
generateSync,
|
||||||
|
} from "./sudoku/index.js";
|
||||||
import stoppable from "stoppable";
|
import stoppable from "stoppable";
|
||||||
|
|
||||||
import cors from "@koa/cors";
|
import cors from "@koa/cors";
|
||||||
@ -31,10 +38,25 @@ async function main() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let sudokuFuncs: any;
|
||||||
|
if (process.env.USE_WORKER_THREADS) {
|
||||||
|
initializeWorkers();
|
||||||
|
sudokuFuncs = {
|
||||||
|
solve,
|
||||||
|
generate,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
sudokuFuncs = {
|
||||||
|
solve: solveSync,
|
||||||
|
generate: generateSync,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
applyGraphQL({
|
applyGraphQL({
|
||||||
app,
|
app,
|
||||||
typeDefs: typeDefs,
|
typeDefs: typeDefs,
|
||||||
resolvers: resolvers,
|
resolvers: resolvers,
|
||||||
|
context: () => sudokuFuncs,
|
||||||
});
|
});
|
||||||
|
|
||||||
runtime.server = stoppable(
|
runtime.server = stoppable(
|
||||||
|
@ -49,13 +49,12 @@ function spawnWorker() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initialize() {
|
export function initializeWorkers() {
|
||||||
console.log(`Starting ${WORKERS} worker threads`);
|
console.log(`Starting ${WORKERS} worker threads`);
|
||||||
for (let n = 0; n < WORKERS; n++) {
|
for (let n = 0; n < WORKERS; n++) {
|
||||||
spawnWorker();
|
spawnWorker();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
initialize();
|
|
||||||
|
|
||||||
function pickWorker() {
|
function pickWorker() {
|
||||||
const proxy = available.pop();
|
const proxy = available.pop();
|
||||||
@ -165,7 +164,7 @@ export function solveSync({
|
|||||||
regionWidth,
|
regionWidth,
|
||||||
regionHeight,
|
regionHeight,
|
||||||
cells,
|
cells,
|
||||||
}: SolveArguments) {
|
}: SolveArguments): Sudoku {
|
||||||
const size = (regionWidth * regionHeight) ** 2;
|
const size = (regionWidth * regionHeight) ** 2;
|
||||||
if (size !== cells.length) {
|
if (size !== cells.length) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
Loading…
Reference in New Issue
Block a user