Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a local keyword registry to make validate thread-safe #103

Merged
merged 2 commits into from
Jul 23, 2021

Conversation

carolynvs
Copy link
Contributor

@carolynvs carolynvs commented Jun 2, 2021

This makes validating a schema thread-safe. I was focused on fixing that call path specifically, and not addressing thread-safety throughout the entire code base, so there is may be more global variables being used elsewhere. This PR introduces a a struct to represent a keyword registry, and loading of the registry and validation occurs in instances of a keyword registry, avoiding the use of global package variables.

This is the patch that we have been using for https://github.com/getporter/porter for 6+ months to be able to validate a schema in a thread-safe manner when registering custom validators.

I was mostly interested in getting this to work without making any backwards incompatible changes to this library's existing API. For example, I kept the package level functions that manipulate package variables, such as RegisterKeyword, which requires the use of locks. If you are okay with making a breaking change, and removing global state, that would simplify the patch quite a bit.

This is a fix for #80 , but is limited to validation. There are probably more spots that would need to be refactored as well.

Introduce a keyword registry struct so that most operations are not
acting upon a global package variable.

I have preserved existing behaviors (such as applying the default schema
to the global keyword registry), and kept global functions such as
RegistrKeyword. If breaking changes are allowed, then we could remove
the locks and keyword registry copying which would simplify this patch
quite a bit.

Signed-off-by: Carolyn Van Slyck <[email protected]>
@Arqu
Copy link
Contributor

Arqu commented Jun 2, 2021

Again, very appreciated. This will take a bit more time to review as I have to be more thorough with it and do some testing, but at a glance looks like a step in the right direction.

@carolynvs
Copy link
Contributor Author

@Arqu Just checking if you've had a chance to look at this and had any feedback?

@Arqu
Copy link
Contributor

Arqu commented Jul 5, 2021

Sorry, been pretty booked for the past while. I'll aim to have a go at it within the next 10 days.

@Arqu Arqu self-requested a review July 5, 2021 09:29
@Arqu Arqu self-assigned this Jul 5, 2021
Copy link
Contributor

@Arqu Arqu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the patience, this is exceptional and brings us much closer to being fully thread safe. Before merging, if you could just address the golint issues

draft2019_09_keywords.go:14:1: exported method KeywordRegistry.DefaultIfEmpty should have comment or be unexported
keyword.go:31:6: exported type KeywordRegistry should have comment or be unexported
keyword.go:55:1: exported method KeywordRegistry.Copy should have comment or be unexported

And I'm happy to merge as is.

@carolynvs carolynvs force-pushed the local-keyword-registry2 branch from 4ede4ee to 39ced0c Compare July 22, 2021 14:41
@carolynvs
Copy link
Contributor Author

carolynvs commented Jul 22, 2021

Oh great! I'm excited to not have to have a fork of jsonschema anymore! 🎉 I just pushed a fix for the golint errors.

Copy link
Contributor

@Arqu Arqu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you soo much for the work here, seems like this will help a lot of folks out!

@Arqu Arqu merged commit 2eb22ee into qri-io:master Jul 23, 2021
@carolynvs carolynvs deleted the local-keyword-registry2 branch July 23, 2021 12:08
carolynvs added a commit to carolynvs/cnab-go that referenced this pull request Jul 23, 2021
The PR fixing thread safety when registering a custom keyword has been
merged so I'm removing the fork and going back to using the upstream
repository.

See qri-io/jsonschema#103

Signed-off-by: Carolyn Van Slyck <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants