aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/vespa/messagebus/network/rpcservicepool.h
blob: b45fbff5c25748840dcffc5a8ab0396e32a33fa7 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "rpcservice.h"
#include <vespa/vespalib/stllike/lrucache_map.h>

namespace mbus {

class RPCNetwork;

/**
 * Class used to reuse services for the same pattern when sending messages over
 * the rpc network.
 */
class RPCServicePool {
public:
    RPCServicePool(const RPCServicePool &) = delete;
    RPCServicePool & operator = (const RPCServicePool &) = delete;
    /**
     * Create a new service pool for the given network.
     *
     * @param net     The underlying RPC network.
     * @param maxSize The max number of services to cache.
     */
    RPCServicePool(const slobrok::api::IMirrorAPI & mirror, uint32_t maxSize);

    /**
     * Destructor. Frees any allocated resources.
     */
    ~RPCServicePool();

    /**
     * Returns the RPCServiceAddress that corresponds to a given pattern. This
     * reuses the RPCService object for matching pattern so that load balancing
     * is possible on the network level.
     *
     * @param pattern The pattern for the service we require.
     * @return A service address for the given pattern.
     */
    RPCServiceAddress::UP resolve(const string &pattern);

    /**
     * Returns the number of services available in the pool. This number will
     * never exceed the limit given at construction time.
     *
     * @return The current size of this pool.
     */
    uint32_t getSize() const;

    /**
     * Returns whether or not there is a service available in the pool the
     * corresponds to the given pattern.
     *
     * @param pattern The pattern to check for.
     * @return True if a corresponding service is in the pool.
     */
    bool hasService(const string &pattern) const;
private:
    using ServiceCache = vespalib::lrucache_map< vespalib::LruParam<string, std::shared_ptr<RPCService> >>;
    using LockGuard = std::lock_guard<std::mutex>;

    void handleMirrorUpdates(const LockGuard & guard);

    const slobrok::api::IMirrorAPI & _mirror;
    mutable std::mutex               _lock;
    std::unique_ptr<ServiceCache>    _lru;
    uint32_t                         _updateGen;
    uint32_t                         _maxSize;
};

} // namespace mbus