Skip to content

Commit

Permalink
Added getter/setter for HeartbeatInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedouglas committed May 22, 2009
1 parent ff171e7 commit 8d39aec
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
22 changes: 22 additions & 0 deletions ext/cmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,25 @@ extern "C" void evma_stop_proxy (const char *from)
if (ed)
ed->StopProxy();
}


/***************************
evma_get_heartbeat_interval
****************************/

extern "C" float evma_get_heartbeat_interval()
{
ensure_eventmachine("evma_get_heartbeat_interval");
return EventMachine->GetHeartbeatInterval();
}


/***************************
evma_set_heartbeat_interval
****************************/

extern "C" int evma_set_heartbeat_interval(float interval)
{
ensure_eventmachine("evma_set_heartbeat_interval");
return EventMachine->SetHeartbeatInterval(interval);
}
26 changes: 25 additions & 1 deletion ext/em.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ EventMachine_t::EventMachine_t (void (*event_callback)(const char*, int, const c
bKqueue (false),
kqfd (-1),
epfd (-1),
inotify (NULL)
inotify (NULL),
HeartbeatInterval(2)
{
// Default time-slice is just smaller than one hundred mills.
Quantum.tv_sec = 0;
Expand Down Expand Up @@ -2239,5 +2240,28 @@ void EventMachine_t::_RegisterKqueueFileEvent(int fd)
#endif


/************************************
EventMachine_t::GetHeartbeatInterval
*************************************/

float EventMachine_t::GetHeartbeatInterval()
{
return ((float)HeartbeatInterval / 1000000LL);
}


/************************************
EventMachine_t::SetHeartbeatInterval
*************************************/

int EventMachine_t::SetHeartbeatInterval(float interval)
{
int iv = (int)(interval * 1000000LL);
if (iv > 0) {
HeartbeatInterval = iv;
return 1;
}
return 0;
}
//#endif // OS_UNIX

4 changes: 3 additions & 1 deletion ext/em.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class EventMachine_t
int SubprocessExitStatus;

int GetConnectionCount();
float GetHeartbeatInterval();
int SetHeartbeatInterval(float);

const char *WatchFile (const char*);
void UnwatchFile (int);
Expand Down Expand Up @@ -154,10 +156,10 @@ class EventMachine_t

private:
enum {
HeartbeatInterval = 2,
MaxEpollDescriptors = 64*1024,
MaxEvents = 4096
};
int HeartbeatInterval;
void (*EventCallback)(const char*, int, const char*, int);

class Timer_t: public Bindable_t {
Expand Down
2 changes: 2 additions & 0 deletions ext/eventmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ extern "C" {
void evma_set_max_timer_count (int);
void evma_setuid_string (const char *username);
void evma_stop_machine();
float evma_get_heartbeat_interval();
int evma_set_heartbeat_interval(float);

const char *evma__write_file (const char *filename);
const char *evma_popen (char * const*cmd_strings);
Expand Down
25 changes: 25 additions & 0 deletions ext/rubymain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,29 @@ static VALUE t_stop_proxy (VALUE self, VALUE from)
}


/************************
t_get_heartbeat_interval
*************************/

static VALUE t_get_heartbeat_interval (VALUE self)
{
return rb_float_new(evma_get_heartbeat_interval());
}


/************************
t_set_heartbeat_interval
*************************/

static VALUE t_set_heartbeat_interval (VALUE self, VALUE interval)
{
float iv = RFLOAT_VALUE(interval);
if (evma_set_heartbeat_interval(iv))
return Qtrue;
return Qfalse;
}


/*********************
Init_rubyeventmachine
*********************/
Expand Down Expand Up @@ -980,6 +1003,8 @@ extern "C" void Init_rubyeventmachine()
rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
rb_define_module_function (EmModule, "get_heartbeat_interval", (VALUE(*)(...))t_get_heartbeat_interval, 0);
rb_define_module_function (EmModule, "set_heartbeat_interval", (VALUE(*)(...))t_set_heartbeat_interval, 1);

// Provisional:
rb_define_module_function (EmModule, "_write_file", (VALUE(*)(...))t__write_file, 1);
Expand Down
14 changes: 14 additions & 0 deletions lib/eventmachine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,20 @@ def self.disable_proxy(from)
EM::stop_proxy(from.signature)
end

# Retrieve the heartbeat interval. This is how often EventMachine will check for dead connections
# that have had an InactivityTimeout set via Connection#set_comm_inactivity_timeout.
# Default is 2 seconds.
def self.heartbeat_interval
EM::get_heartbeat_interval
end

# Set the heartbeat interval. This is how often EventMachine will check for dead connections
# that have had an InactivityTimeout set via Connection#set_comm_inactivity_timeout.
# Takes a Numeric number of seconds. Default is 2.
def self.heartbeat_interval= (time)
EM::set_heartbeat_interval time.to_f
end

private

def self.event_callback conn_binding, opcode, data # :nodoc:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,15 @@ def test_schedule_from_thread
end
assert x
end

def test_set_heartbeat_interval
interval = 0.5
EM.run {
EM.set_heartbeat_interval interval
$interval = EM.get_heartbeat_interval
EM.stop
}
assert_equal(interval, $interval)
end
end

0 comments on commit 8d39aec

Please sign in to comment.