aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/message.cpp
blob: 73b72be4fbfc74b56fc901d5d9d1026623f4d02f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "message.h"
#include "reply.h"
#include "ireplyhandler.h"
#include "emptyreply.h"
#include "errorcode.h"
#include "error.h"
#include <vespa/vespalib/util/backtrace.h>

#include <vespa/log/log.h>
LOG_SETUP(".message");

namespace mbus {

Message::Message() :
    _route(),
    _timeReceived(),
    _timeRemaining(0),
    _retryEnabled(true),
    _retry(0)
{
    // By observation there are normally 2 handlers pushed.
    getCallStack().reserve(2);
}

Message::~Message()
{
    if (getCallStack().size() > 0) {
        string backtrace = vespalib::getStackTrace(0);
        LOG(warning, "Deleted message %p with non-empty call-stack. Deleted at:\n%s",
            this, backtrace.c_str());
        auto reply = std::make_unique<EmptyReply>();
        swapState(*reply);
        reply->addError(Error(ErrorCode::TRANSIENT_ERROR,
                              "The message object was deleted while containing state information; "
                              "generating an auto-reply."));
        IReplyHandler &handler = reply->getCallStack().pop(*reply);
        handler.handleReply(std::move(reply));
    }
}

void
Message::swapState(Routable &rhs)
{
    Routable::swapState(rhs);
    if (!rhs.isReply()) {
        Message &msg = static_cast<Message&>(rhs);

        std::swap(_route, msg._route);
        std::swap(_retryEnabled, msg._retryEnabled);
        std::swap(_retry, msg._retry);
        std::swap(_timeReceived, msg._timeReceived);
        std::swap(_timeRemaining, msg._timeRemaining);
    }
}

Message &
Message::setTimeReceivedNow()
{
    _timeReceived = vespalib::steady_clock::now();
    return *this;
}

duration
Message::getTimeRemainingNow() const
{
    return std::max(0ns, _timeRemaining - (vespalib::steady_clock::now() - _timeReceived));
}

} // namespace mbus