diff --git a/package.json b/package.json index 8fe8abd..34243ec 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "version": "1.0.1", "description": "基于nodejs worker_threads的线程池。耗时操作或nodejs没有提供异步模式的api(例如解密、同步的文件api)都可以在线程池中执行,业务代码只需要返回一个Promise或async函数给线程池库,至于业务逻辑做什么操作,其实都可以,比如setTimeout,异步操作,async await等", "main": "src/index.js", + "module": "src/index.js", + "types": "src/index.d.ts", "directories": { "test": "test" }, diff --git a/src/config.d.ts b/src/config.d.ts new file mode 100644 index 0000000..8a61a99 --- /dev/null +++ b/src/config.d.ts @@ -0,0 +1,4 @@ +export declare const MAX_THREADS = 50; +export declare const MAX_WORK: number; +export declare const CORE_THREADS = 10; +export declare const MAX_IDLE_TIME: number; diff --git a/src/constants.d.ts b/src/constants.d.ts new file mode 100644 index 0000000..db15548 --- /dev/null +++ b/src/constants.d.ts @@ -0,0 +1,18 @@ +export declare const DISCARD_POLICY: { + ABORT: number; + CALLER_RUN: number; + OLDEST_DISCARD: number; + DISCARD: number; + NOT_DISCARD: number; +}; +export declare const THREAD_STATE: { + IDLE: number; + BUSY: number; + DEAD: number; +}; +export declare const WORK_STATE: { + PENDDING: number; + RUNNING: number; + END: number; + CANCELED: number; +}; diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..dcc2bbb --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,4 @@ +export * as constants from './constants'; +export * as config from './config'; +export * as threadPool from './threadPool'; +export { defaultCpuThreadPool, defaultFixedThreadPool, defaultSingleThreadPool, defaultThreadPool, ThreadPool, CPUThreadPool, FixedThreadPool, SingleThreadPool } from './threadPool'; diff --git a/src/index.js b/src/index.js index 9d65afd..3a87e3f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,8 @@ +const threadPool = require('./threadPool'); + module.exports = { constants: require('./constants'), config: require('./config'), - threadPool: require('./threadPool'), + threadPool, + ...threadPool }; \ No newline at end of file diff --git a/src/threadPool.d.ts b/src/threadPool.d.ts new file mode 100644 index 0000000..b81bd0c --- /dev/null +++ b/src/threadPool.d.ts @@ -0,0 +1,84 @@ +/// +import { Worker } from 'worker_threads'; +import { EventEmitter } from 'events'; +import { Work } from './work'; +interface IPropsOptions { + workId: number; +} +declare class UserWork extends EventEmitter { + workId: number; + timer: any; + state: number; + terminate?: () => void; + constructor({ workId }: IPropsOptions); + setTimeout(timeout: number): void; + clearTimeout(): void; + cancel(): boolean; + setState(state: number): void; +} +interface IThreadOptions { + worker: Worker; +} +declare class Thread { + worker: Worker; + threadId: number; + state: number; + lastWorkTime: number; + constructor({ worker }: IThreadOptions); + setState(state: number): void; + setLastWorkTime(time: number): void; +} +interface IThreadPoolOptions { + coreThreads?: number; + maxThreads?: number; + discardPolicy?: number; + preCreate?: boolean; + timeout?: number; + maxIdleTime?: number; + maxWork?: number; + expansion?: boolean; +} +export declare class ThreadPool { + options: IThreadPoolOptions; + workerQueue: Thread[]; + coreThreads: number; + maxThreads: number; + discardPolicy: number; + preCreate: boolean; + maxIdleTime: number; + workPool: { + [props: number]: UserWork; + }; + workId: number; + queue: Work[]; + totalWork: number; + maxWork: number; + timeout: number; + constructor(options?: IThreadPoolOptions); + pollIdle(): void; + preCreateThreads(): void; + newThread(): Thread; + selectThead(): Thread; + generateWorkId(): number; + submit(filename: string, options?: { + [props: string]: any; + }): Promise; + submitWorkToThread(thread: Thread, work: Work): void; + addWork(userWork: UserWork): void; + endWork(userWork: UserWork): void; + cancelWork(userWork: UserWork): void; +} +export declare class CPUThreadPool extends ThreadPool { + constructor(options?: IThreadPoolOptions); +} +export declare class SingleThreadPool extends ThreadPool { + constructor(options?: IThreadPoolOptions); +} +export declare class FixedThreadPool extends ThreadPool { + constructor(options?: IThreadPoolOptions); +} +export declare const defaultThreadPool: ThreadPool; +export declare const defaultCpuThreadPool: CPUThreadPool; +export declare const defaultFixedThreadPool: FixedThreadPool; +export declare const defaultSingleThreadPool: SingleThreadPool; +export {}; diff --git a/src/utils.d.ts b/src/utils.d.ts new file mode 100644 index 0000000..05d078d --- /dev/null +++ b/src/utils.d.ts @@ -0,0 +1,3 @@ +export declare function isFunction(func: unknown): boolean; +export declare function isJSFile(file: string): boolean; +export declare function isMJSFile(file: string): boolean; diff --git a/src/work.d.ts b/src/work.d.ts new file mode 100644 index 0000000..6b6cb3c --- /dev/null +++ b/src/work.d.ts @@ -0,0 +1,16 @@ +interface IPropsOption { + workId: number; + filename: string; + options: { + [props: string]: any; + }; +} +export declare class Work { + workId: number; + filename: string; + data: any; + error: any; + options: any; + constructor({ workId, filename, options }: IPropsOption); +} +export {}; diff --git a/src/worker.d.ts b/src/worker.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/src/worker.d.ts @@ -0,0 +1 @@ +export {};