From 78bdc8103ff4c1b2f1f636f9c5564285028f3e19 Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Wed, 26 Nov 2014 13:51:02 +0100 Subject: [PATCH] [REST] give an appropriate response in warmup phase --- src/rest.cpp | 4 ++++ src/rpcprotocol.h | 1 + src/rpcserver.cpp | 8 ++++++++ src/rpcserver.h | 3 +++ 4 files changed, 16 insertions(+) diff --git a/src/rest.cpp b/src/rest.cpp index 1aa864a306..4953d7e717 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -188,6 +188,10 @@ bool HTTPReq_REST(AcceptedConnection *conn, bool fRun) { try { + std::string statusmessage; + if(RPCIsInWarmup(&statusmessage)) + throw RESTERR(HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: "+statusmessage); + for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) { unsigned int plen = strlen(uri_prefixes[i].prefix); if (strURI.substr(0, plen) == uri_prefixes[i].prefix) { diff --git a/src/rpcprotocol.h b/src/rpcprotocol.h index a321338176..f7cd50f9f6 100644 --- a/src/rpcprotocol.h +++ b/src/rpcprotocol.h @@ -28,6 +28,7 @@ enum HTTPStatusCode HTTP_FORBIDDEN = 403, HTTP_NOT_FOUND = 404, HTTP_INTERNAL_SERVER_ERROR = 500, + HTTP_SERVICE_UNAVAILABLE = 503, }; //! Bitcoin RPC error codes diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 7022c50375..c10b05cb04 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -756,6 +756,14 @@ void SetRPCWarmupFinished() fRPCInWarmup = false; } +bool RPCIsInWarmup(std::string *outStatus) +{ + LOCK(cs_rpcWarmup); + if (outStatus) + *outStatus = rpcWarmupStatus; + return fRPCInWarmup; +} + void RPCRunHandler(const boost::system::error_code& err, boost::function func) { if (!err) diff --git a/src/rpcserver.h b/src/rpcserver.h index 7395fc23c6..b0e437057b 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -53,6 +53,9 @@ void SetRPCWarmupStatus(const std::string& newStatus); /* Mark warmup as done. RPC calls will be processed from now on. */ void SetRPCWarmupFinished(); +/* returns the current warmup state. */ +bool RPCIsInWarmup(std::string *statusOut); + /** * Type-check arguments; throws JSONRPCError if wrong type given. Does not check that * the right number of arguments are passed, just that any passed are the correct type.