File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 5
5
6
6
#include " remote/i2-remote.hpp"
7
7
#include " remote/endpoint-ti.hpp"
8
+ #include " base/atomic.hpp"
8
9
#include " base/ringbuffer.hpp"
10
+ #include < cstdint>
9
11
#include < set>
12
+ #include < shared_mutex>
13
+ #include < unordered_map>
10
14
11
15
namespace icinga
12
16
{
13
17
18
+ class ApiFunction ;
14
19
class JsonRpcConnection ;
15
20
class Zone ;
16
21
@@ -21,6 +26,8 @@ class Zone;
21
26
*/
22
27
class Endpoint final : public ObjectImpl<Endpoint>
23
28
{
29
+ friend JsonRpcConnection;
30
+
24
31
public:
25
32
DECLARE_OBJECT (Endpoint);
26
33
DECLARE_OBJECTNAME (Endpoint);
@@ -61,6 +68,9 @@ class Endpoint final : public ObjectImpl<Endpoint>
61
68
mutable RingBuffer m_MessagesReceived{60 };
62
69
mutable RingBuffer m_BytesSent{60 };
63
70
mutable RingBuffer m_BytesReceived{60 };
71
+
72
+ mutable std::shared_mutex m_MessageCountersMutex;
73
+ std::unordered_map<intrusive_ptr<ApiFunction>, Atomic<uint_fast64_t >> m_MessageCounters;
64
74
};
65
75
66
76
}
Original file line number Diff line number Diff line change @@ -351,6 +351,25 @@ void JsonRpcConnection::MessageHandler(const Dictionary::Ptr& message)
351
351
Log (LogNotice, " JsonRpcConnection" )
352
352
<< " Call to non-existent function '" << method << " ' from endpoint '" << m_Identity << " '." ;
353
353
} else {
354
+ if (m_Endpoint) {
355
+ std::shared_lock sLock (m_Endpoint->m_MessageCountersMutex );
356
+ auto & mc (m_Endpoint->m_MessageCounters );
357
+ auto it (mc.find (afunc.get ())); // Lookup by pointer is faster than by string
358
+
359
+ if (it == mc.end ()) {
360
+ sLock .unlock ();
361
+ std::unique_lock uLock (m_Endpoint->m_MessageCountersMutex );
362
+
363
+ if (it = mc.find (afunc.get ()); it == mc.end ()) {
364
+ mc.emplace (afunc.get (), 1 );
365
+ } else {
366
+ it->second .fetch_add (1 , std::memory_order_relaxed);
367
+ }
368
+ } else {
369
+ it->second .fetch_add (1 , std::memory_order_relaxed);
370
+ }
371
+ }
372
+
354
373
Dictionary::Ptr params = message->Get (" params" );
355
374
if (params)
356
375
resultMessage->Set (" result" , afunc->Invoke (origin, params));
You can’t perform that action at this time.
0 commit comments