From: Eric Wong <e@80x24.org>
To: yahns-public@yhbt.net
Subject: [PATCH 2/3] extras/proxy_pass: log exceptions leading to 502
Date: Tue, 3 Mar 2015 07:59:29 +0000 [thread overview]
Message-ID: <1425369570-28427-3-git-send-email-e@80x24.org> (raw)
In-Reply-To: <1425369570-28427-1-git-send-email-e@80x24.org>
It may be useful for us to track down potential errors in
our code or log when an upstream misbehaves.
---
| 4 +++
test/server_helper.rb | 6 ++--
| 78 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 3 deletions(-)
create mode 100644 test/test_extras_proxy_pass.rb
--git a/extras/proxy_pass.rb b/extras/proxy_pass.rb
index 8a32cac..de6a2b1 100644
--- a/extras/proxy_pass.rb
+++ b/extras/proxy_pass.rb
@@ -189,6 +189,10 @@ class ProxyPass # :nodoc:
res
rescue => e
retry if ures && ures.fail_retryable? && request_method != "POST"
+ if defined?(Yahns::Log)
+ logger = env['rack.logger'] and
+ Yahns::Log.exception(logger, 'proxy_pass', e)
+ end
ERROR_502
end
diff --git a/test/server_helper.rb b/test/server_helper.rb
index e2641a9..1ce2f29 100644
--- a/test/server_helper.rb
+++ b/test/server_helper.rb
@@ -72,10 +72,10 @@ module ServerHelper
@ru = nil
end
- def mkserver(cfg)
+ def mkserver(cfg, srv = @srv)
fork do
- ENV["YAHNS_FD"] = @srv.fileno.to_s
- @srv.autoclose = false
+ ENV["YAHNS_FD"] = srv.fileno.to_s
+ srv.autoclose = false
yield if block_given?
Yahns::Server.new(cfg).start.join
end
--git a/test/test_extras_proxy_pass.rb b/test/test_extras_proxy_pass.rb
new file mode 100644
index 0000000..513e574
--- /dev/null
+++ b/test/test_extras_proxy_pass.rb
@@ -0,0 +1,78 @@
+# Copyright (C) 2015 all contributors <yahns-public@yhbt.net>
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require_relative 'server_helper'
+
+class TestExtrasProxyPass < Testcase
+ ENV["N"].to_i > 1 and parallelize_me!
+ include ServerHelper
+
+ class ProxiedApp
+ def call(env)
+ h = [ %w(Content-Length 3), %w(Content-Type text/plain) ]
+ case env['REQUEST_METHOD']
+ when 'GET'
+ [ 200, h, [ "hi\n"] ]
+ when 'HEAD'
+ [ 200, h, [] ]
+ when 'PUT'
+ buf = env['rack.input'].read
+ [ 201, {
+ 'Content-Length' => buf.bytesize.to_s,
+ 'Content-Type' => 'text/plain',
+ }, [ buf ] ]
+ end
+ end
+ end
+
+ def setup
+ @srv2 = TCPServer.new(ENV["TEST_HOST"] || "127.0.0.1", 0)
+ server_helper_setup
+ end
+
+ def teardown
+ @srv2.close if defined?(@srv2) && !@srv2.closed?
+ server_helper_teardown
+ end
+
+ def test_proxy_pass
+ err, cfg, host, port = @err, Yahns::Config.new, @srv.addr[3], @srv.addr[1]
+ host2, port2 = @srv2.addr[3], @srv2.addr[1]
+ pid = mkserver(cfg) do
+ $LOAD_PATH.unshift "#{Dir.pwd}/extras"
+ require 'proxy_pass'
+ @srv2.close
+ cfg.instance_eval do
+ app(:rack, ProxyPass.new("http://#{host2}:#{port2}/")) do
+ listen "#{host}:#{port}"
+ end
+ stderr_path err.path
+ end
+ end
+
+ pid2 = mkserver(cfg, @srv2) do
+ @srv.close
+ cfg.instance_eval do
+ app(:rack, ProxiedApp.new) do
+ listen "#{host2}:#{port2}"
+ end
+ stderr_path err.path
+ end
+ end
+
+ Net::HTTP.start(host, port) do |http|
+ res = http.request(Net::HTTP::Get.new('/'))
+ assert_equal 200, res.code.to_i
+ n = res.body.bytesize
+ assert_operator n, :>, 1
+ res = http.request(Net::HTTP::Head.new('/'))
+ assert_equal 200, res.code.to_i
+ assert_equal n, res['Content-Length'].to_i
+ assert_nil res.body
+
+ res = http.put(Net::HTTP::Put.new('/'))
+ end
+ ensure
+ quit_wait pid
+ quit_wait pid2
+ end
+end
--
EW
next prev parent reply other threads:[~2015-03-03 7:59 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-03 7:59 [PUSHED] extras/proxy_pass updates Eric Wong
2015-03-03 7:59 ` [PATCH 1/3] extras/proxy_pass: do not name unused variable Eric Wong
2015-03-03 7:59 ` Eric Wong [this message]
2015-03-03 7:59 ` [PATCH 3/3] extras/proxy_pass: flesh out upload support + tests Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://yhbt.net/yahns/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1425369570-28427-3-git-send-email-e@80x24.org \
--to=e@80x24.org \
--cc=yahns-public@yhbt.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhbt.net/yahns.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).