Skip to content

Commit 78bdc81

Browse files
committed
[REST] give an appropriate response in warmup phase
1 parent 210eba9 commit 78bdc81

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

src/rest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ bool HTTPReq_REST(AcceptedConnection *conn,
188188
bool fRun)
189189
{
190190
try {
191+
std::string statusmessage;
192+
if(RPCIsInWarmup(&statusmessage))
193+
throw RESTERR(HTTP_SERVICE_UNAVAILABLE, "Service temporarily unavailable: "+statusmessage);
194+
191195
for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) {
192196
unsigned int plen = strlen(uri_prefixes[i].prefix);
193197
if (strURI.substr(0, plen) == uri_prefixes[i].prefix) {

src/rpcprotocol.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enum HTTPStatusCode
2828
HTTP_FORBIDDEN = 403,
2929
HTTP_NOT_FOUND = 404,
3030
HTTP_INTERNAL_SERVER_ERROR = 500,
31+
HTTP_SERVICE_UNAVAILABLE = 503,
3132
};
3233

3334
//! Bitcoin RPC error codes

src/rpcserver.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,14 @@ void SetRPCWarmupFinished()
756756
fRPCInWarmup = false;
757757
}
758758

759+
bool RPCIsInWarmup(std::string *outStatus)
760+
{
761+
LOCK(cs_rpcWarmup);
762+
if (outStatus)
763+
*outStatus = rpcWarmupStatus;
764+
return fRPCInWarmup;
765+
}
766+
759767
void RPCRunHandler(const boost::system::error_code& err, boost::function<void(void)> func)
760768
{
761769
if (!err)

src/rpcserver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ void SetRPCWarmupStatus(const std::string& newStatus);
5353
/* Mark warmup as done. RPC calls will be processed from now on. */
5454
void SetRPCWarmupFinished();
5555

56+
/* returns the current warmup state. */
57+
bool RPCIsInWarmup(std::string *statusOut);
58+
5659
/**
5760
* Type-check arguments; throws JSONRPCError if wrong type given. Does not check that
5861
* the right number of arguments are passed, just that any passed are the correct type.

0 commit comments

Comments
 (0)