about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-02-08 17:14:41 -0800
committerEric Wong <normalperson@yhbt.net>2010-02-08 17:14:41 -0800
commit2dd3e22782090dc347429e306569f687fa794d06 (patch)
tree89dc09a023bb6a40a18859ae133491b7bbb0e332
parent74ab49d2f02d65fb4d2915563d8b4c4f026e4657 (diff)
downloadclogger-2dd3e22782090dc347429e306569f687fa794d06.tar.gz
Clogger may set this value independently of "rack.multithread"
since Actor/Fiber-based servers may have multiple instances of
Clogger wrapping different response bodies and yet be
incompatible with "rack.multithread"
-rw-r--r--ext/clogger_ext/clogger.c13
-rw-r--r--lib/clogger/pure.rb4
-rw-r--r--test/test_clogger.rb42
3 files changed, 57 insertions, 2 deletions
diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index f2d82d6..da6b5d2 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -599,6 +599,19 @@ static VALUE clogger_init(int argc, VALUE *argv, VALUE self)
                 tmp = rb_hash_aref(o, ID2SYM(rb_intern("format")));
                 if (!NIL_P(tmp))
                         fmt = tmp;
+
+                tmp = rb_hash_aref(o, ID2SYM(rb_intern("reentrant")));
+                switch (TYPE(tmp)) {
+                case T_TRUE:
+                        c->reentrant = 1;
+                        break;
+                case T_FALSE:
+                        c->reentrant = 0;
+                case T_NIL:
+                        break;
+                default:
+                        rb_raise(rb_eArgError, ":reentrant must be boolean");
+                }
         }
 
         init_buffers(c);
diff --git a/lib/clogger/pure.rb b/lib/clogger/pure.rb
index 0c609ba..b871c62 100644
--- a/lib/clogger/pure.rb
+++ b/lib/clogger/pure.rb
@@ -13,7 +13,7 @@ class Clogger
     (@logger.sync = true) rescue nil
     @fmt_ops = compile_format(opts[:format] || Format::Common, opts)
     @wrap_body = need_wrap_body?(@fmt_ops)
-    @reentrant = nil
+    @reentrant = opts[:reentrant]
     @need_resp = need_response_headers?(@fmt_ops)
     @body_bytes_sent = 0
   end
@@ -28,7 +28,7 @@ class Clogger
     status, headers, body = resp
     headers = Rack::Utils::HeaderHash.new(headers) if @need_resp
     if @wrap_body
-      @reentrant = env['rack.multithread']
+      @reentrant = env['rack.multithread'] if @reentrant.nil?
       @env, @status, @headers, @body = env, status, headers, body
       return [ status, headers, @reentrant ? self.dup : self ]
     end
diff --git a/test/test_clogger.rb b/test/test_clogger.rb
index 1906718..b086bbb 100644
--- a/test/test_clogger.rb
+++ b/test/test_clogger.rb
@@ -545,4 +545,46 @@ class TestClogger < Test::Unit::TestCase
     assert_equal :foo, body.close
   end
 
+  def test_clogger_auto_reentrant_true
+    s = ''
+    body = []
+    app = lambda { |env| [302, [ %w(a) ], body ] }
+    cl = Clogger.new(app, :logger => s, :format => "$request_time")
+    @req['rack.multithread'] = true
+    status, headers, body = cl.call(@req)
+    assert cl.reentrant?
+  end
+
+  def test_clogger_auto_reentrant_false
+    s = ''
+    body = []
+    app = lambda { |env| [302, [ %w(a) ], body ] }
+    cl = Clogger.new(app, :logger => s, :format => "$request_time")
+    @req['rack.multithread'] = false
+    status, headers, body = cl.call(@req)
+    assert ! cl.reentrant?
+  end
+
+  def test_clogger_auto_reentrant_forced_true
+    s = ''
+    body = []
+    app = lambda { |env| [302, [ %w(a) ], body ] }
+    o = { :logger => s, :format => "$request_time", :reentrant => true }
+    cl = Clogger.new(app, o)
+    @req['rack.multithread'] = false
+    status, headers, body = cl.call(@req)
+    assert cl.reentrant?
+  end
+
+  def test_clogger_auto_reentrant_forced_false
+    s = ''
+    body = []
+    app = lambda { |env| [302, [ %w(a) ], body ] }
+    o = { :logger => s, :format => "$request_time", :reentrant => false }
+    cl = Clogger.new(app, o)
+    @req['rack.multithread'] = true
+    status, headers, body = cl.call(@req)
+    assert ! cl.reentrant?
+  end
+
 end