-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
172c4ef
commit 55d57e3
Showing
4 changed files
with
150 additions
and
24 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,17 @@ | ||
#!/usr/bin/env ruby | ||
# encoding: utf-8 | ||
|
||
require "bundler" | ||
Bundler.setup | ||
|
||
$:.unshift(File.expand_path("../../../lib", __FILE__)) | ||
|
||
require 'bunny' | ||
|
||
|
||
b = Bunny.new(:heartbeat_interval => 2) | ||
b.start | ||
|
||
c = b.create_channel | ||
|
||
sleep 10 |
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,59 @@ | ||
require "thread" | ||
require "amq/protocol/client" | ||
require "amq/protocol/frame" | ||
|
||
module Bunny | ||
class HeartbeatSender | ||
|
||
# | ||
# API | ||
# | ||
|
||
def initialize(session) | ||
@session = session | ||
@mutex = Mutex.new | ||
|
||
@last_activity_time = Time.now | ||
end | ||
|
||
def start(period = 30) | ||
@mutex.synchronize do | ||
@period = period | ||
|
||
@thread = Thread.new(&method(:run)) | ||
end | ||
end | ||
|
||
def stop | ||
@mutex.synchronize { @thread.exit } | ||
end | ||
|
||
def signal_activity! | ||
@last_activity_time = Time.now | ||
end | ||
|
||
protected | ||
|
||
def run | ||
begin | ||
loop do | ||
self.beat | ||
|
||
sleep (@period / 2) | ||
end | ||
rescue IOError => ioe | ||
# ignored | ||
rescue Exception => e | ||
puts e.message | ||
end | ||
end | ||
|
||
def beat | ||
now = Time.now | ||
|
||
if now > (@last_activity_time + @period) | ||
@session.send_raw(AMQ::Protocol::HeartbeatFrame.encode) | ||
end | ||
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
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