This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Use channels do deal with memory concurrency
By using a Channel is expected that the memory get no concurrent access, the Fibers should send all the requests over the Channel. The same will receive and perform in the received order. Additional changes: - Add a 'request' abstraction, to deal with parsing the received message and handling commands
- Loading branch information
1 parent
80eedd8
commit 5b721d1
Showing
2 changed files
with
52 additions
and
36 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "socket" | ||
require "logger" | ||
require "./logger" | ||
require "./command" | ||
require "./memory" | ||
|
||
module BoJack | ||
class Request | ||
@logger : ::Logger = BoJack::Logger.instance | ||
def initialize(@body : String, @socket : TCPSocket, @memory : BoJack::Memory(String, Array(String))); end | ||
|
||
def perform | ||
@logger.info("#{@socket.remote_address} requested: #{@body.strip}") | ||
params = parse(@body) | ||
command = BoJack::Command.from(params[:command]) | ||
|
||
response = command.run(@socket, @memory, params) | ||
|
||
@socket.puts(response) | ||
rescue e | ||
message = "error: #{e.message}" | ||
@logger.error(message) | ||
@socket.puts(message) | ||
end | ||
|
||
private def parse(body) : Hash(Symbol, String | Array(String)) | ||
body = body.split(" ").map { |item| item.strip } | ||
|
||
command = body[0] | ||
result = Hash(Symbol, String | Array(String)).new | ||
result[:command] = command | ||
|
||
result[:key] = body[1] if body[1]? | ||
result[:value] = body[2].split(",") if body[2]? | ||
|
||
result | ||
end | ||
end | ||
end |
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
5b721d1
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.
π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π π
5b721d1
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.
Sorry :P
5b721d1
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.
@mauricioabreu thanks ahahah