Skip to content

Commit

Permalink
Merge pull request #62 from rage-rb/fiber-id
Browse files Browse the repository at this point in the history
Preload fiber ID
  • Loading branch information
rsamoilov authored Feb 21, 2024
2 parents 2926fff + 77934ac commit c5b05a7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
13 changes: 7 additions & 6 deletions lib/rage/fiber.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class Fiber
# @private
AWAIT_ERROR_MESSAGE = "err"

# @private
Expand All @@ -23,14 +24,14 @@ def __get_err
@__err
end

# @private
def __get_id
@__rage_id ||= object_id.to_s
# @private
def __set_id
@__rage_id = object_id.to_s
end

# @private
def __yielded?
!@__rage_id.nil?
# @private
def __get_id
@__rage_id
end

# @private
Expand Down
1 change: 1 addition & 0 deletions lib/rage/fiber_scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def fiber(&block)
fiber = if parent == @root_fiber
# the fiber to wrap a request in
Fiber.new(blocking: false) do
Fiber.current.__set_id
Fiber.current.__set_result(block.call)
end
else
Expand Down
2 changes: 1 addition & 1 deletion lib/rage/middleware/fiber_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def call(env)
@app.call(env)
ensure
# notify Iodine the request can now be resumed
Iodine.publish(Fiber.current.__get_id, "", Iodine::PubSub::PROCESS) if Fiber.current.__yielded?
Iodine.publish(Fiber.current.__get_id, "", Iodine::PubSub::PROCESS)
end

# the fiber encountered blocking IO and yielded; instruct Iodine to pause the request
Expand Down
1 change: 1 addition & 0 deletions lib/rage/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class Fiber
def self.schedule(&block)
fiber = Fiber.new(blocking: true) do
Fiber.current.__set_id
Fiber.current.__set_result(block.call)
end
fiber.resume
Expand Down

0 comments on commit c5b05a7

Please sign in to comment.