summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/ReapThread.cpp
blob: 649c5092795a0511ede4f64b46b7bf7a4219f957 (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include <Ice/Ice.h>
#include <IceGrid/ReapThread.h>

using namespace std;
using namespace IceGrid;

ReapThread::ReapThread() :
    _closeCallback([this](const auto& con) { connectionClosed(con); }),
    _heartbeatCallback([this](const auto& con) { connectionHeartbeat(con); }),
    _terminated(false),
    _thread([this] { run(); })
{
}

void
ReapThread::run()
{
    vector<ReapableItem> reap;
    while(true)
    {
        {
            unique_lock lock(_mutex);
            if(_terminated)
            {
                break;
            }

            calcWakeInterval();

            //
            // If the wake interval is zero then we wait forever.
            //
            if(_wakeInterval == 0s)
            {
                _condVar.wait(lock);
            }
            else
            {
                _condVar.wait_for(lock, _wakeInterval);
            }

            if(_terminated)
            {
                break;
            }

            auto p = _sessions.begin();
            while(p != _sessions.end())
            {
                try
                {
                    if(p->timeout == 0s)
                    {
                        p->item->timestamp(); // This should throw if the reapable is destroyed.
                        ++p;
                        continue;
                    }
                    else if((chrono::steady_clock::now() - p->item->timestamp()) > p->timeout)
                    {
                        reap.push_back(*p);
                    }
                    else
                    {
                        ++p;
                        continue;
                    }
                }
                catch(const Ice::ObjectNotExistException&)
                {
                }

                //
                // Remove the reapable
                //
                if(p->connection)
                {
                    auto q = _connections.find(p->connection);
                    if(q != _connections.end())
                    {
                        q->second.erase(p->item);
                        if(q->second.empty())
                        {
                            p->connection->setCloseCallback(nullptr);
                            p->connection->setHeartbeatCallback(nullptr);
                            _connections.erase(q);
                        }
                    }
                }
                p = _sessions.erase(p);
            }
        }

        for(const auto& r : reap)
        {
            r.item->destroy(false);
        }
        reap.clear();
    }
}

void
ReapThread::terminate()
{
    list<ReapableItem> reap;
    {
        lock_guard lock(_mutex);
        if(_terminated)
        {
            assert(_sessions.empty());
            return;
        }
        _terminated = true;
        _condVar.notify_one();
        reap.swap(_sessions);

        for(const auto& conn : _connections)
        {
            conn.first->setCloseCallback(nullptr);
            conn.first->setHeartbeatCallback(nullptr);
        }
        _connections.clear();
        _closeCallback = nullptr;
        _heartbeatCallback = nullptr;
    }

    for(const auto& r : reap)
    {
        r.item->destroy(true);
    }
}

void
ReapThread::join()
{
    _thread.join();
}

void
ReapThread::add(const shared_ptr<Reapable>& reapable, chrono::seconds timeout,
                const shared_ptr<Ice::Connection>& connection)
{
    lock_guard lock(_mutex);
    if(_terminated)
    {
        return;
    }

    //
    // NOTE: registering a reapable with a null timeout is allowed. The reapable is reaped
    // only when the reaper thread is shutdown.
    //

    //
    // 10 seconds is the minimum permissable timeout.
    //
    if(timeout > 0s && timeout < 10s)
    {
        timeout = 10s;
    }

    _sessions.push_back({ reapable, connection, timeout });

    if(connection)
    {
        auto p = _connections.find(connection);
        if(p == _connections.end())
        {
            p = _connections.insert({connection, {} }).first;
            connection->setCloseCallback(_closeCallback);
            connection->setHeartbeatCallback(_heartbeatCallback);
        }
        p->second.insert(reapable);
    }

    if(timeout > 0s)
    {
        //
        // If there is a new minimum wake interval then wake the reaping
        // thread.
        //
        if(calcWakeInterval())
        {
            _condVar.notify_one();
        }

        //
        // Since we just added a new session with a non null timeout there
        // must be a non-zero wakeInterval.
        //
        assert(_wakeInterval != 0s);
    }
}

void
ReapThread::connectionHeartbeat(const shared_ptr<Ice::Connection>& con)
{
    lock_guard lock(_mutex);

    auto p = _connections.find(con);
    if(p == _connections.end())
    {
        con->setCloseCallback(nullptr);
        con->setHeartbeatCallback(nullptr);
        return;
    }

    for(const auto& reapable : p->second)
    {
        reapable->heartbeat();
    }
}

void
ReapThread::connectionClosed(const shared_ptr<Ice::Connection>& con)
{
    lock_guard lock(_mutex);

    auto p = _connections.find(con);
    if(p == _connections.end())
    {
        con->setCloseCallback(nullptr);
        con->setHeartbeatCallback(nullptr);
        return;
    }

    for(const auto& reapable : p->second)
    {
        reapable->destroy(false);
    }
    _connections.erase(p);
}

//
// Returns true if the calculated wake interval is less than the current wake
// interval (or if the original wake interval was "forever").
//
bool
ReapThread::calcWakeInterval()
{
    // Re-calculate minimum timeout
    auto oldWakeInterval = _wakeInterval;
    chrono::milliseconds minimum = 0s;
    bool first = true;
    for(const auto& session : _sessions)
    {
        if(session.timeout != 0s && (first || session.timeout < minimum))
        {
            minimum = session.timeout;
            first = false;
        }
    }

    _wakeInterval = minimum;
    return oldWakeInterval == 0s || minimum < oldWakeInterval;
}