Class VertxContextSafetyToggle

java.lang.Object
io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle

public final class VertxContextSafetyToggle extends Object
This is meant for other extensions to integrate with, to help identify which Contexts are isolated and guaranteeing access by a unique thread or event chain; for example it's used by Hibernate Reactive to check if the current context is safe for it to store the currently opened session, to protect users from mistakenly interleaving multiple reactive operations which could unintentionally share the same session. Vert.x web will create a new context for each http web request; Quarkus RestEasy Reactive will mark such contexts as safe. Other extensions should follow a similar pattern when they are setting up a new context which is safe to be used for the purposes of a local context guaranteeing sequential use, non-concurrent access and scoped to the current reactive chain as a convenience to not have to pass a "context" object along explicitly. In other cases it might be useful to explicitly mark the current context as not safe instead; for example if an existing context needs to be shared across multiple workers to process some operations in parallel: by marking and un-marking appropriately the same context can have spans in which it's safe, followed by spans in which it's not safe. Normally we would expect the user to know and follow the caveats expressed in the documentation of each project, but this additional facility helps to catch errors. The current context can be explicitly marked as safe, or it can be explicitly marked as unsafe; there's a third state which is the default of any new context: unmarked. The default is to consider any unmarked Context to be unsafe unless the system property UNRESTRICTED_BY_DEFAULT_PROPERTY is set to "true"; flip the default if you know what you're doing and can't flag the context explicitly, but bear in mind that this will have a global impact and possibly let other uses go unnoticed as well.
  • Field Details

    • UNRESTRICTED_BY_DEFAULT_PROPERTY

      public static final String UNRESTRICTED_BY_DEFAULT_PROPERTY
      See Also:
    • FULLY_DISABLE_PROPERTY

      public static final String FULLY_DISABLE_PROPERTY
      This gets exposed for people who prefer fully disabling all safeguards, for example because they have tested it all carefully under load already and are preferring maximum efficiency over the safeguards introduced by this class.
      See Also:
  • Constructor Details

    • VertxContextSafetyToggle

      public VertxContextSafetyToggle()
  • Method Details

    • validateContextIfExists

      public static void validateContextIfExists(String errorMessageOnVeto, String errorMessageOnDoubt)
      Verifies if the current Vert.x context was flagged as safe to be accessed by components which expect non-concurrent access to the current context, and its state isolated to the current stream. For example, it checks if it's suitable to store a Session for Hibernate Reactive.
      Parameters:
      errorMessageOnVeto - the message to use for an IllegalStateException, should the context be explicitly marked as unsafe.
      errorMessageOnDoubt - the message to use for an IllegalStateException, in case the current context has no markers and flag RESTRICT_BY_DEFAULT has been set.
      Throws:
      IllegalStateException - if the context exists and it failed to be validated
    • setCurrentContextSafe

      public static void setCurrentContextSafe(boolean safe)
      Parameters:
      safe - set to true to explicitly mark the current context as safe, or false to explicitly mark it as unsafe.
      Throws:
      IllegalStateException - if there is no current context, or if it's of the wrong type.
    • setContextSafe

      public static void setContextSafe(io.vertx.core.Context context, boolean safe)
      Parameters:
      safe - set to true to explicitly mark the current context as safe, or false to explicitly mark it as unsafe.
      context - The context to mark.
      Throws:
      IllegalStateException - if context is null or not of the expected type.
    • isExplicitlyMarkedAsSafe

      public static boolean isExplicitlyMarkedAsSafe(io.vertx.core.Context context)
    • isExplicitlyMarkedAsUnsafe

      public static boolean isExplicitlyMarkedAsUnsafe(io.vertx.core.Context context)