~unity-api-team/unity-scopes-api/child-scopes-option

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2013 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Michi Henning <michi.henning@canonical.com>
 */

#include <unity/scopes/internal/zmq_middleware/ConnectionPool.h>

#include <unity/scopes/ScopeExceptions.h>

#include <cassert>

using namespace std;

namespace unity
{

namespace scopes
{

namespace internal
{

namespace zmq_middleware
{

ConnectionPool::ConnectionPool(zmqpp::context& context)
    : context_(context)
{
}

ConnectionPool::~ConnectionPool()
{
    pool_.clear();
}

zmqpp::socket& ConnectionPool::find(std::string const& endpoint)
{
    assert(!endpoint.empty());

    // Look for existing connection.
    auto const& it = pool_.find(endpoint);
    if (it != pool_.end())
    {
        return it->second;
    }

    // No existing connection yet, establish one.
    auto s = create_connection(endpoint);
    return pool_.emplace(make_pair(endpoint, move(s))).first->second;
}

zmqpp::socket ConnectionPool::create_connection(std::string const& endpoint)
{
    zmqpp::socket s(context_, zmqpp::socket_type::push);
    // Allow short linger time so messages written just before we shut down
    // have some chance of being sent, and we don't block indefinitely if the
    // peer has gone away.
    s.set(zmqpp::socket_option::linger, 50);
    // We set a reconnect interval of 20 ms, so we get to the peer quickly, in case
    // the peer hasn't finished binding to its endpoint yet after the first query
    // is sent. We back off exponentially to one second.
    // TODO: This still doesn't entirely stop the reconnection attempts that are
    //       made by Zmq behind the scenes. We'll have to add a garbage collection
    //       thread that closes outgoing connections after some idle time.
    s.set(zmqpp::socket_option::reconnect_interval, 20);
    s.set(zmqpp::socket_option::reconnect_interval_max, 1000);
    s.connect(endpoint);
    return move(s);
}

void ConnectionPool::remove(std::string const& endpoint)
{
    assert(!endpoint.empty());

    auto const& it = pool_.find(endpoint);
    if (it != pool_.end())
    {
        pool_.erase(it);
    }
}

void ConnectionPool::register_socket(std::string const& endpoint, zmqpp::socket socket)
{
    assert(!endpoint.empty());

    pool_.emplace(endpoint, move(socket));
}

} // namespace zmq_middleware

} // namespace internal

} // namespace scopes

} // namespace unity