-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor concurrent processing and handling of jobs
fetch is now reduced to one async tail call with execution thrown onto the event loop. to control concurrency we check the in progress against the concurrency limit and Promise.race the in progress jobs to know when we can continue fetching.
- Loading branch information
Showing
13 changed files
with
123 additions
and
124 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
import { EventEmitter } from "koa"; | ||
|
||
declare module "generic-pool" { | ||
export interface Pool<T> { | ||
ready(): PromiseLike<void>; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,7 +25,6 @@ test("invokes middleware", async (t) => { | |
}, | ||
}, | ||
}); | ||
|
||
worker.work(); | ||
}); | ||
}); | ||
|
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,41 @@ | ||
import compose, { ComposedMiddleware } from "koa-compose"; | ||
import { Middleware, MiddlewareContext, Registry } from "./worker"; | ||
|
||
/** | ||
* Builds a koa-compose stack of the middleware functions in addition to | ||
* two worker-added middleware functions for pulling the job function from the | ||
* registry and calling the job function and/or thunk | ||
* | ||
* @private | ||
* @return {function} entrypoint function to the middleware stack | ||
*/ | ||
|
||
export default function createExecutionChain( | ||
middleware: Middleware[], | ||
registry: Registry | ||
): ComposedMiddleware<MiddlewareContext> { | ||
return compose([ | ||
...middleware, | ||
function getJobFnFromRegistry(ctx, next) { | ||
const { | ||
job: { jobtype }, | ||
} = ctx; | ||
ctx.fn = registry[jobtype]; | ||
return next(); | ||
}, | ||
async function callJobFn(ctx, next) { | ||
const { | ||
fn, | ||
job: { jobtype, args }, | ||
} = ctx; | ||
if (!fn) throw new Error(`No jobtype registered: ${jobtype}`); | ||
const thunkOrPromise = await fn(...args); | ||
if (typeof thunkOrPromise === "function") { | ||
await thunkOrPromise(ctx); | ||
} else { | ||
await thunkOrPromise; | ||
} | ||
return next(); | ||
}, | ||
]); | ||
} |
Oops, something went wrong.