-
-
Notifications
You must be signed in to change notification settings - Fork 541
feat: add config.isolation_level for fiber-safe hub storage (Falcon/async) #3018
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
Open
SeanLF
wants to merge
3
commits into
getsentry:master
Choose a base branch
from
SeanLF:feat/fiber-hub-storage
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| # Stores the SDK's current {Hub} in an execution-context-local slot. | ||
| # | ||
| # The isolation level decides which execution primitive owns the hub: | ||
| # | ||
| # [+:thread+ (default)] The hub lives in thread-local storage | ||
| # (+Thread#thread_variable_get+/+thread_variable_set+). This is the SDK's | ||
| # historical behaviour and is correct for thread-based servers (Puma, | ||
| # Unicorn) and background processors (Sidekiq, Resque). Every fiber running | ||
| # on a thread shares one hub. | ||
| # | ||
| # [+:fiber+] The hub lives in Fiber Storage (+Fiber#[]+, Ruby 3.2+). Each | ||
| # fiber gets its own hub, and a newly created fiber inherits a copy of its | ||
| # parent's storage, so context still propagates into child fibers (for | ||
| # example graphql-ruby resolvers or +Async+ tasks) instead of being lost. | ||
| # This is the correct level for fiber-based servers such as Falcon, where | ||
| # many concurrent requests run as sibling fibers on a single thread and must | ||
| # not share a hub. | ||
| # | ||
| # Both levels use the same storage key (+Sentry::THREAD_LOCAL+, value | ||
| # +:sentry_hub+), kept stable for backwards compatibility because integrations | ||
| # and user code reference it directly. | ||
| # | ||
| # @api private | ||
| module HubStorage | ||
| # Isolation levels the SDK understands. | ||
| LEVELS = %i[thread fiber].freeze | ||
|
|
||
| class << self | ||
| # @return [Symbol] the currently active isolation level. | ||
| attr_reader :isolation_level | ||
|
|
||
| # @param level [Symbol] +:thread+ or +:fiber+. | ||
| # @raise [ArgumentError] if +level+ is not a known isolation level. | ||
| # @return [Symbol] the level that was applied. | ||
| def isolation_level=(level) | ||
| @isolation_level = normalize_isolation_level(level) | ||
| end | ||
|
|
||
| # Validates a requested isolation level and downgrades +:fiber+ to | ||
| # +:thread+ when Fiber Storage is unavailable (Ruby < 3.2), so the storage | ||
| # boundary can never be asked to call +Fiber[]+ on a Ruby that lacks it. | ||
| # | ||
| # @param level [Symbol] | ||
| # @raise [ArgumentError] if +level+ is not a known isolation level. | ||
| # @return [Symbol] the level that is safe to apply. | ||
| def normalize_isolation_level(level) | ||
| unless LEVELS.include?(level) | ||
| raise ArgumentError, "isolation_level must be one of #{LEVELS.inspect}, got #{level.inspect}" | ||
| end | ||
|
|
||
| level == :fiber && !fiber_storage_available? ? :thread : level | ||
| end | ||
|
|
||
| # @return [Hub, nil] the hub stored for the current execution context. | ||
| def get | ||
| if @isolation_level == :fiber | ||
| ::Fiber[THREAD_LOCAL] | ||
| else | ||
| ::Thread.current.thread_variable_get(THREAD_LOCAL) | ||
| end | ||
| end | ||
|
|
||
| # @param hub [Hub, nil] | ||
| # @return [Hub, nil] | ||
| def set(hub) | ||
| if @isolation_level == :fiber | ||
| ::Fiber[THREAD_LOCAL] = hub | ||
| else | ||
| ::Thread.current.thread_variable_set(THREAD_LOCAL, hub) | ||
| end | ||
| end | ||
|
|
||
| # Clears the hub for the current execution context. | ||
| # @return [void] | ||
| def clear | ||
| set(nil) | ||
| end | ||
|
|
||
| # Whether the running Ruby exposes Fiber Storage (+Fiber#[]+, Ruby 3.2+), | ||
| # the primitive the +:fiber+ isolation level is built on. | ||
| # @return [Boolean] | ||
| def fiber_storage_available? | ||
| ::Fiber.respond_to?(:[]) && ::Fiber.respond_to?(:[]=) | ||
| end | ||
| end | ||
|
|
||
| self.isolation_level = :thread | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.