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
|
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/RetryQueue.h>
#include <Ice/OutgoingAsync.h>
#include <Ice/LocalException.h>
#include <Ice/Instance.h>
#include <Ice/TraceLevels.h>
#include <Ice/LoggerUtil.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
IceUtil::Shared* IceInternal::upCast(RetryQueue* p) { return p; }
IceInternal::RetryTask::RetryTask(const InstancePtr& instance,
const RetryQueuePtr& queue,
const ProxyOutgoingAsyncBasePtr& outAsync) :
_instance(instance),
_queue(queue),
_outAsync(outAsync)
{
}
void
IceInternal::RetryTask::runTimerTask()
{
_outAsync->retry(); // Retry again the invocation.
//
// NOTE: this must be called last, destroy() blocks until all task
// are removed to prevent the client thread pool to be destroyed
// (we still need the client thread pool at this point to call
// exception callbacks with CommunicatorDestroyedException).
//
_queue->remove(this);
}
void
IceInternal::RetryTask::requestCanceled(OutgoingBase*, const Ice::LocalException&)
{
assert(false);
}
void
IceInternal::RetryTask::asyncRequestCanceled(const OutgoingAsyncBasePtr& outAsync, const Ice::LocalException& ex)
{
if(_queue->cancel(this))
{
if(_instance->traceLevels()->retry >= 1)
{
Trace out(_instance->initializationData().logger, _instance->traceLevels()->retryCat);
out << "operation retry canceled\n" << ex;
}
if(_outAsync->completed(ex))
{
_outAsync->invokeCompletedAsync();
}
}
}
void
IceInternal::RetryTask::destroy()
{
try
{
_outAsync->abort(CommunicatorDestroyedException(__FILE__, __LINE__));
}
catch(const CommunicatorDestroyedException&)
{
// Abort can throw if there's no callback, ignore.
}
}
bool
IceInternal::RetryTask::operator<(const RetryTask& rhs) const
{
return this < &rhs;
}
IceInternal::RetryQueue::RetryQueue(const InstancePtr& instance) : _instance(instance)
{
}
void
IceInternal::RetryQueue::add(const ProxyOutgoingAsyncBasePtr& out, int interval)
{
Lock sync(*this);
if(!_instance)
{
throw CommunicatorDestroyedException(__FILE__, __LINE__);
}
RetryTaskPtr task = new RetryTask(_instance, this, out);
out->cancelable(task); // This will throw if the request is canceled.
try
{
_instance->timer()->schedule(task, IceUtil::Time::milliSeconds(interval));
}
catch(const IceUtil::IllegalArgumentException&) // Expected if the communicator destroyed the timer.
{
throw CommunicatorDestroyedException(__FILE__, __LINE__);
}
_requests.insert(task);
}
void
IceInternal::RetryQueue::destroy()
{
Lock sync(*this);
assert(_instance);
set<RetryTaskPtr>::iterator p = _requests.begin();
while(p != _requests.end())
{
if(_instance->timer()->cancel(*p))
{
(*p)->destroy();
_requests.erase(p++);
}
else
{
++p;
}
}
_instance = 0;
while(!_requests.empty())
{
wait();
}
}
void
IceInternal::RetryQueue::remove(const RetryTaskPtr& task)
{
Lock sync(*this);
assert(_requests.find(task) != _requests.end());
_requests.erase(task);
if(!_instance && _requests.empty())
{
notify(); // If we are destroying the queue, destroy is probably waiting on the queue to be empty.
}
}
bool
IceInternal::RetryQueue::cancel(const RetryTaskPtr& task)
{
Lock sync(*this);
if(_requests.erase(task) > 0)
{
if(!_instance && _requests.empty())
{
notify(); // If we are destroying the queue, destroy is probably waiting on the queue to be empty.
}
return _instance->timer()->cancel(task);
}
return false;
}
|