aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/acceptor_status.cpp
blob: 5006ff68aee86c79b1e71ee3f472ef8a7aec7137 (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
#include "acceptor_status.h"

#include "key_value_printer.h"

#include <util/stream/format.h>
#include <util/stream/output.h>

using namespace NBus;
using namespace NBus::NPrivate;

TAcceptorStatus::TAcceptorStatus()
    : Summary(false)
    , AcceptorId(0)
    , Fd(INVALID_SOCKET)
{
    ResetIncremental();
}

void TAcceptorStatus::ResetIncremental() {
    AcceptSuccessCount = 0;
    AcceptErrorCount = 0;
    LastAcceptErrorErrno = 0;
    LastAcceptErrorInstant = TInstant();
    LastAcceptSuccessInstant = TInstant();
}

TAcceptorStatus& TAcceptorStatus::operator+=(const TAcceptorStatus& that) {
    Y_ASSERT(Summary);
    Y_ASSERT(AcceptorId == 0);

    AcceptSuccessCount += that.AcceptSuccessCount;
    LastAcceptSuccessInstant = Max(LastAcceptSuccessInstant, that.LastAcceptSuccessInstant);

    AcceptErrorCount += that.AcceptErrorCount;
    if (that.LastAcceptErrorInstant > LastAcceptErrorInstant) {
        LastAcceptErrorInstant = that.LastAcceptErrorInstant;
        LastAcceptErrorErrno = that.LastAcceptErrorErrno;
    }

    return *this;
}

TString TAcceptorStatus::PrintToString() const {
    TStringStream ss;

    if (!Summary) {
        ss << "acceptor (" << AcceptorId << "), fd=" << Fd << ", addr=" << ListenAddr << Endl;
    }

    TKeyValuePrinter p;

    p.AddRow("accept error count", LeftPad(AcceptErrorCount, 4));

    if (AcceptErrorCount > 0) {
        p.AddRow("last accept error",
                 TString() + LastSystemErrorText(LastAcceptErrorErrno) + " at " + LastAcceptErrorInstant.ToString());
    }

    p.AddRow("accept success count", LeftPad(AcceptSuccessCount, 4));
    if (AcceptSuccessCount > 0) {
        p.AddRow("last accept success",
                 TString() + "at " + LastAcceptSuccessInstant.ToString());
    }

    ss << p.PrintToString();

    return ss.Str();
}