You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2024/10/29 19:44:27 UTC
(couchdb) 01/01: allow client to specify request id
This is an automated email from the ASF dual-hosted git repository.
rnewson pushed a commit to branch glum-x-couch-request-id
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit 50e49e0f40fbf74b1544e33d359a6476387964cd
Author: Robert Newson <rn...@apache.org>
AuthorDate: Tue Oct 29 16:48:59 2024 +0000
allow client to specify request id
This allows tracing of requests that fail to start in a timely
manner when using a load balancer in front.
---
src/chttpd/src/chttpd.erl | 42 +++++++++++++++++++++++++++++++++++-
test/elixir/test/basics_test.exs | 6 ++++++
test/elixir/test/config/suite.elixir | 3 ++-
3 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/src/chttpd/src/chttpd.erl b/src/chttpd/src/chttpd.erl
index 4770335eb..af3b29969 100644
--- a/src/chttpd/src/chttpd.erl
+++ b/src/chttpd/src/chttpd.erl
@@ -96,6 +96,11 @@
-define(DEFAULT_SERVER_OPTIONS, "[{recbuf, undefined}]").
-define(DEFAULT_SOCKET_OPTIONS, "[{sndbuf, 262144}, {nodelay, true}]").
+%% copied from mochiweb_util.erl
+-define(IS_HEX(C), ((C >= $0 andalso C =< $9) orelse
+ (C >= $a andalso C =< $f) orelse
+ (C >= $A andalso C =< $F))).
+
start_link() ->
start_link(http).
start_link(http) ->
@@ -306,7 +311,7 @@ handle_request_int(MochiReq) ->
Other -> Other
end,
- Nonce = couch_util:to_hex(crypto:strong_rand_bytes(5)),
+ Nonce = nonce(MochiReq),
HttpReq0 = #httpd{
mochi_req = MochiReq,
@@ -431,6 +436,41 @@ handle_req_after_auth(HandlerKey, HttpReq) ->
{HttpReq, catch_error(HttpReq, ErrorType, Error, Stack)}
end.
+nonce(MochiReq) ->
+ case MochiReq:get_header_value("X-Couch-Request-ID") of
+ undefined ->
+ new_nonce();
+ Value when length(Value) > 10 ->
+ new_nonce();
+ Value ->
+ case lists:all(fun is_hex/1, Value) of
+ true ->
+ Value;
+ false ->
+ new_nonce()
+ end
+ end.
+
+new_nonce() ->
+ couch_util:to_hex(crypto:strong_rand_bytes(5)).
+
+is_hex(C) when ?IS_HEX(C) ->
+ true;
+is_hex(_) ->
+ false.
+
+nonce_regex() ->
+ Key = chttpd_request_id_regex,
+ Regex = config:get("chttpd", "request_id_regex", "^[a-zA-Z0-9-_]{1,64}+$"),
+ case persistent_term:get(Key, {nil, nil}) of
+ {Regex, Compiled} ->
+ Compiled;
+ {_OtherRegex, _OtherCompiled} ->
+ {ok, Compiled} = re:compile(Regex),
+ persistent_term:put(Key, {Regex, Compiled}),
+ Compiled
+ end.
+
catch_error(_HttpReq, throw, {http_head_abort, Resp}, _Stack) ->
{ok, Resp};
catch_error(_HttpReq, throw, {http_abort, Resp, Reason}, _Stack) ->
diff --git a/test/elixir/test/basics_test.exs b/test/elixir/test/basics_test.exs
index abc66ca40..e3f4a76c7 100644
--- a/test/elixir/test/basics_test.exs
+++ b/test/elixir/test/basics_test.exs
@@ -381,4 +381,10 @@ defmodule BasicsTest do
assert head_response.headers["X-Couch-Request-ID"]
assert head_response.headers["X-CouchDB-Body-Time"]
end
+
+ @tag :with_db
+ test "request ID can be specified at the client", context do
+ resp = Couch.get("/", headers: ["X-Couch-Request-ID": "123abc"])
+ assert resp.headers["X-Couch-Request-ID"] == "123abc"
+ end
end
diff --git a/test/elixir/test/config/suite.elixir b/test/elixir/test/config/suite.elixir
index a4199e65b..04c1f7748 100644
--- a/test/elixir/test/config/suite.elixir
+++ b/test/elixir/test/config/suite.elixir
@@ -81,7 +81,8 @@
"Welcome endpoint",
"_all_docs POST error when multi-get is not a {'key': [...]} structure",
"_bulk_docs POST error when body not an object",
- "oops, the doc id got lost in code nirwana"
+ "oops, the doc id got lost in code nirwana",
+ "request ID can be specified at the client"
],
"BatchSaveTest": [
"batch post",