From ba61b659aec23053d42de21d81e4b1694c382a7c Mon Sep 17 00:00:00 2001
From: Chris Wong <chris@chriswongstudio.com>
Date: Fri, 5 Mar 2010 09:36:45 -0600
Subject: [PATCH 2/2] Expose close method in Socket explicitly

GC-based language like Ruby should explicitly close the socket to avoid leak.
Coupling the deallocation of the socket with the GC's sweeping phase will
eventually cause problem in high-load system.
---
 rbzmq.cpp |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/rbzmq.cpp b/rbzmq.cpp
index 109b33f..13cde7d 100644
--- a/rbzmq.cpp
+++ b/rbzmq.cpp
@@ -226,6 +226,19 @@ static VALUE socket_recv (VALUE self_, VALUE flags_)
     return message;
 }
 
+static VALUE socket_close (VALUE self_)
+{
+    void * s = NULL;
+    Data_Get_Struct(self_, void, s);
+    int rc = zmq_close(s);
+    if (rc != 0) {
+        rb_raise (rb_eRuntimeError, zmq_strerror (zmq_errno ()));
+        return Qnil;
+    }
+
+    return Qnil;
+}
+
 extern "C" void Init_zmq ()
 {
     VALUE zmq_module = rb_define_module ("ZMQ");
@@ -251,6 +264,8 @@ extern "C" void Init_zmq ()
         (VALUE(*)(...)) socket_flush, 0);
     rb_define_method (socket_type, "recv",
         (VALUE(*)(...)) socket_recv, 1);
+    rb_define_method (socket_type, "close",
+        (VALUE(*)(...)) socket_close, 0);
 
     rb_define_const (zmq_module, "HWM", INT2NUM (ZMQ_HWM));
     rb_define_const (zmq_module, "LWM", INT2NUM (ZMQ_LWM));
-- 
1.6.4.2

