Skip to content

Commit

Permalink
Merge pull request #242 from harrel56/chore/sync-block-opt-out
Browse files Browse the repository at this point in the history
Replace synchronized with ReentrantLock - prevent virtual threads getting pinned
  • Loading branch information
harrel56 authored Oct 14, 2024
2 parents a364e6a + 78a36a5 commit 03d7fe0
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/main/java/dev/harrel/jsonschema/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URI;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
Expand All @@ -13,6 +14,7 @@ final class JsonParser {
private final SchemaRegistry schemaRegistry;
private final MetaSchemaValidator metaSchemaValidator;
private final boolean disabledSchemaValidation;
private final ReentrantLock lock = new ReentrantLock();
private final Map<URI, UnfinishedSchema> unfinishedSchemas = new HashMap<>();

JsonParser(Map<URI, Dialect> dialects,
Expand All @@ -29,13 +31,18 @@ final class JsonParser {
this.disabledSchemaValidation = disabledSchemaValidation;
}

synchronized URI parseRootSchema(URI baseUri, JsonNode node) {
SchemaRegistry.State snapshot = schemaRegistry.createSnapshot();
URI parseRootSchema(URI baseUri, JsonNode node) {
lock.lock();
try {
return parseRootSchemaInternal(UriUtil.getUriWithoutFragment(baseUri), node);
} catch (RuntimeException e) {
schemaRegistry.restoreSnapshot(snapshot);
throw e;
SchemaRegistry.State snapshot = schemaRegistry.createSnapshot();
try {
return parseRootSchemaInternal(UriUtil.getUriWithoutFragment(baseUri), node);
} catch (RuntimeException e) {
schemaRegistry.restoreSnapshot(snapshot);
throw e;
}
} finally {
lock.unlock();
}
}

Expand Down

0 comments on commit 03d7fe0

Please sign in to comment.