blob: a31440399926c8e5bc9b4dd71c012f4a6431774a (
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
72
73
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "rpcservicepool.h"
#include "rpcnetwork.h"
#include <vespa/vespalib/stllike/lrucache_map.hpp>
namespace mbus {
RPCServicePool::RPCServicePool(const slobrok::api::IMirrorAPI & mirror, uint32_t maxSize) :
_mirror(mirror),
_lock(),
_lru(std::make_unique<ServiceCache>(maxSize)),
_updateGen(0),
_maxSize(maxSize)
{
_lru->reserve(maxSize);
assert(maxSize > 0);
}
RPCServicePool::~RPCServicePool() = default;
RPCServiceAddress::UP
RPCServicePool::resolve(const string &pattern)
{
std::shared_ptr<RPCService> service;
{
LockGuard guard(_lock);
handleMirrorUpdates(guard);
std::shared_ptr<RPCService> *found = _lru->findAndRef(pattern);
if (found) {
service = *found;
}
}
if (service) {
return service->make_address();
} else {
service = std::make_shared<RPCService>(_mirror, pattern);
auto result = service->make_address();
if (service->isValid()) {
LockGuard guard(_lock);
(*_lru)[pattern] = std::move(service);
}
return result;
}
}
void
RPCServicePool::handleMirrorUpdates(const LockGuard &) {
uint32_t currentgen = _mirror.updates();
if (_updateGen != currentgen) {
auto lru = std::make_unique<ServiceCache>(_maxSize);
_lru.swap(lru);
_updateGen = currentgen;
}
}
uint32_t
RPCServicePool::getSize() const
{
LockGuard guard(_lock);
return _lru->size();
}
bool
RPCServicePool::hasService(const string &pattern) const
{
LockGuard guard(_lock);
return _lru->hasKey(pattern);
}
} // namespace mbus
|