Navigation Menu

Skip to content

Commit

Permalink
Add mechanism to skip forwarding of obsolete messages
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 8, 2015
1 parent 6fcc5b4 commit e6a37e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/droonga/engine_node.rb
Expand Up @@ -34,6 +34,8 @@ def initialize(name, state, loop, params)
@sender_node_metadata = params[:metadata]

@buffer = ForwardBuffer.new(name)
boundary_timestamp = accept_messages_newer_than_timestamp
@buffer.process_messages_newer_than(boundary_timestamp)
@buffer.on_forward = lambda do |message, destination|
output(message, destination)
end
Expand Down
22 changes: 22 additions & 0 deletions lib/droonga/forward_buffer.rb
Expand Up @@ -18,6 +18,7 @@
require "fileutils"
require "pathname"
require "msgpack"
require "time"

require "droonga/loggable"
require "droonga/path"
Expand Down Expand Up @@ -74,15 +75,36 @@ def empty?
@data_directory.children.empty?
end

def process_messages_newer_than(timestamp)
@process_messages_newer_than_timestamp = timestamp
end

private
def forward(buffered_message_path)
logger.trace("forward: start (#{buffered_message_path})")
file_contents = buffered_message_path.read
@unpacker.feed(file_contents)
buffered_message = @unpacker.read
@unpacker.reset

if @process_messages_newer_than_timestamp
message_timestamp = Time.parse(message["date"])
logger.trace("Checking boundary of obsolete message",
:newer_than => @process_messages_newer_than_timestamp,
:message_at => message_timestamp)
if @process_messages_newer_than_timestamp >= message_timestamp
buffered_message = nil
else
logger.info("New message is detected. The boundary is now cleared.")
@process_messages_newer_than_timestamp = nil
end
end

if buffered_message
on_forward(buffered_message["message"],
buffered_message["destination"])
end

FileUtils.rm_f(buffered_message_path.to_s)
logger.trace("forward: done (#{buffered_message_path})")
end
Expand Down

0 comments on commit e6a37e5

Please sign in to comment.