Skip to content

Commit

Permalink
feat: Update ajv-threads package to version with worker pool (#3177)
Browse files Browse the repository at this point in the history
This will make schema validation in js-ceramic truly parallel for the first time
  • Loading branch information
stbrody committed Mar 12, 2024
1 parent b8c2a3a commit 3a1e0db
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"@stablelib/random": "^1.0.1",
"@stablelib/sha256": "^1.0.1",
"@stablelib/uuid": "^1.0.1",
"ajv-threads": "^1.0.3",
"ajv-threads": "^1.0.4",
"await-semaphore": "^0.1.3",
"cartonne": "^3.0.1",
"codeco": "^1.1.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/ceramic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {

this._ipfs = modules.ipfs

this._schemaValidator = new SchemaValidation()
const numCores = os.cpus().length
// Use number of threads equal to half the available cores for schema validation. Leave the
// other half for signature validation.
this._schemaValidator = new SchemaValidation(Math.max(1, Math.ceil(numCores / 2)))
this._streamHandlers = HandlersMap.makeWithDefaultHandlers(this._logger, this._schemaValidator)

// This initialization block below has to be redone.
Expand Down Expand Up @@ -484,6 +487,7 @@ export class Ceramic implements StreamReaderWriter, StreamStateLoader {
this._logger.warn(`Starting in read-only mode. All write operations will fail`)
}

await this._schemaValidator.init()
await this.repository.init()
await this.dispatcher.init()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ describeIfV3('StateManipulator test', () => {
const logger = new LoggerProvider().getDiagnosticsLogger()
logSyncer = new LogSyncer(dispatcher)

schemaValidator = new SchemaValidation()
schemaValidator = new SchemaValidation(1)
await schemaValidator.init()
const handlers = HandlersMap.makeWithDefaultHandlers(logger, schemaValidator)
stateManipulator = new StateManipulator(logger, handlers, logSyncer, ceramic)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ describeIfV3('Streamloader', () => {
ceramic.anchorService.validator
)

schemaValidator = new SchemaValidation()
schemaValidator = new SchemaValidation(1)
await schemaValidator.init()
const handlers = HandlersMap.makeWithDefaultHandlers(logger, schemaValidator)
const stateManipulator = new StateManipulator(logger, handlers, logSyncer, ceramic)
streamLoader = new StreamLoader(
Expand Down
2 changes: 1 addition & 1 deletion packages/stream-model-instance-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@ceramicnetwork/stream-model": "^4.2.0-rc.0",
"@ceramicnetwork/stream-model-instance": "^4.3.0-rc.0",
"@ceramicnetwork/streamid": "^5.0.0",
"ajv-threads": "^1.0.3",
"ajv-threads": "^1.0.4",
"fast-json-patch": "^3.1.0",
"least-recent": "^1.0.3",
"lodash.clonedeep": "^4.5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ describe('ModelInstanceDocumentHandler', () => {
let ipfs: IpfsApi

beforeAll(async () => {
schemaValidator = new SchemaValidation()
schemaValidator = new SchemaValidation(1)
await schemaValidator.init()
const recs: Record<string, any> = {}
ipfs = {
dag: {
Expand Down

0 comments on commit 3a1e0db

Please sign in to comment.