This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Implements concurrent requests #48
Closed
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a6bc1e9
Implements concurrent requests using channels, select and sleep
faustinoaq 150f867
Move content outside spawn block
faustinoaq d651877
Add request counter to debug logs
faustinoaq e4a823b
Add another request counter log
faustinoaq 162164c
Change counter debug message
faustinoaq 73034a5
Fix request counter
faustinoaq 90ae85a
Use buffered channel
faustinoaq 823fb41
Add newline
faustinoaq ef85166
Increment channel buffer
faustinoaq 97b44dc
Trying to implement a concurrent rpc for scry
faustinoaq 820d7d3
Apply suggestions
faustinoaq 4d1c897
Merge branch 'master' into fa/implement-concurrent-requests
faustinoaq 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 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 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,69 @@ | ||
module Scry | ||
class ConcurrentRpc | ||
CONTEXT = Context.new | ||
|
||
private def get_requests(reader, input_channel) | ||
Log.logger.info "getting request..." | ||
content = Request.new(reader).read | ||
Log.logger.info "getting content..." | ||
input_channel.send content | ||
end | ||
|
||
private def handle_request(content, result_channel) | ||
Log.logger.info "Processing request..." | ||
Log.logger.info content | ||
request = Message.new(content).parse | ||
results = CONTEXT.dispatch(request) | ||
rescue ex | ||
results = [ResponseMessage.new(ex)] | ||
ensure | ||
Log.logger.info results | ||
Log.logger.info "End Processing request..." | ||
result_channel.send [results].flatten | ||
end | ||
|
||
def run | ||
Log.logger.info "server has started" | ||
input_channel = Channel(String | Nil).new | ||
|
||
spawn get_requests(reader, input_channel) | ||
|
||
channels = [] of Channel(String | Nil) | Channel(Array(Result)) | ||
channels << input_channel | ||
|
||
until channels.empty? | ||
channel_index, data = Channel.select(channels.map &.receive_select_action) | ||
Log.logger.info "data received" | ||
Log.logger.info channel_index | ||
Log.logger.info data | ||
|
||
if channels[channel_index] == input_channel | ||
if data.nil? | ||
Log.logger.info "Input closed, no more data to come in current channel..." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
channels.delete_at(channel_index) | ||
next | ||
end | ||
|
||
content = data.as(String) | ||
result_channel = Channel(Array(Result)).new | ||
|
||
spawn handle_request(content, result_channel) | ||
|
||
channels << result_channel | ||
else | ||
results = data.as(Array(Result)) | ||
Log.logger.info "Printing result output..." | ||
Log.logger.info results | ||
response = Response.new(results) | ||
response.write(writer) | ||
channels.delete_at(channel_index) | ||
end | ||
end | ||
rescue ex | ||
Log.logger.error(ex.inspect_with_backtrace) unless Log.logger.nil? | ||
ensure | ||
Log.logger.info("...your session has ended") | ||
Log.logger.close unless Log.logger.nil? | ||
end | ||
end | ||
end |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure it's safe to have the context global to all the fibers?
(not sure what the Context object does in scry)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well,I tried to use
@context
as well, but still doesn't work, see comment below 👇