| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | |
|
| | 5 | | namespace Ice.Internal; |
| | 6 | |
|
| | 7 | | public class RetryTask : TimerTask, CancellationHandler |
| | 8 | | { |
| | 9 | | public RetryTask(Instance instance, RetryQueue retryQueue, ProxyOutgoingAsyncBase outAsync) |
| | 10 | | { |
| | 11 | | _instance = instance; |
| | 12 | | _retryQueue = retryQueue; |
| | 13 | | _outAsync = outAsync; |
| | 14 | | } |
| | 15 | |
|
| | 16 | | public void runTimerTask() |
| | 17 | | { |
| | 18 | | _outAsync.retry(); |
| | 19 | |
|
| | 20 | | // |
| | 21 | | // NOTE: this must be called last, destroy() blocks until all task |
| | 22 | | // are removed to prevent the client thread pool to be destroyed |
| | 23 | | // (we still need the client thread pool at this point to call |
| | 24 | | // exception callbacks with CommunicatorDestroyedException). |
| | 25 | | // |
| | 26 | | _retryQueue.remove(this); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public void asyncRequestCanceled(OutgoingAsyncBase outAsync, Ice.LocalException ex) |
| | 30 | | { |
| | 31 | | Debug.Assert(_outAsync == outAsync); |
| | 32 | | if (_retryQueue.cancel(this)) |
| | 33 | | { |
| | 34 | | if (_instance.traceLevels().retry >= 1) |
| | 35 | | { |
| | 36 | | _instance.initializationData().logger.trace( |
| | 37 | | _instance.traceLevels().retryCat, |
| | 38 | | $"operation retry canceled\n{ex}"); |
| | 39 | | } |
| | 40 | | if (_outAsync.exception(ex)) |
| | 41 | | { |
| | 42 | | _outAsync.invokeExceptionAsync(); |
| | 43 | | } |
| | 44 | | } |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public void destroy() |
| | 48 | | { |
| | 49 | | try |
| | 50 | | { |
| | 51 | | _outAsync.abort(new Ice.CommunicatorDestroyedException()); |
| | 52 | | } |
| | 53 | | catch (Ice.CommunicatorDestroyedException) |
| | 54 | | { |
| | 55 | | // Abort can throw if there's no callback, just ignore in this case |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | private readonly Instance _instance; |
| | 60 | | private readonly RetryQueue _retryQueue; |
| | 61 | | private readonly ProxyOutgoingAsyncBase _outAsync; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public class RetryQueue |
| | 65 | | { |
| 1 | 66 | | public RetryQueue(Instance instance) => _instance = instance; |
| | 67 | |
|
| | 68 | | public void add(ProxyOutgoingAsyncBase outAsync, int interval) |
| | 69 | | { |
| 1 | 70 | | lock (_mutex) |
| | 71 | | { |
| 1 | 72 | | if (_instance == null) |
| | 73 | | { |
| 0 | 74 | | throw new Ice.CommunicatorDestroyedException(); |
| | 75 | | } |
| 1 | 76 | | var task = new RetryTask(_instance, this, outAsync); |
| 1 | 77 | | outAsync.cancelable(task); // This will throw if the request is canceled. |
| 1 | 78 | | _instance.timer().schedule(task, interval); |
| 1 | 79 | | _requests.Add(task, null); |
| 1 | 80 | | } |
| 1 | 81 | | } |
| | 82 | |
|
| | 83 | | public void destroy() |
| | 84 | | { |
| 1 | 85 | | lock (_mutex) |
| | 86 | | { |
| 1 | 87 | | var keep = new Dictionary<RetryTask, object>(); |
| 1 | 88 | | foreach (RetryTask task in _requests.Keys) |
| | 89 | | { |
| 1 | 90 | | if (_instance.timer().cancel(task)) |
| | 91 | | { |
| 0 | 92 | | task.destroy(); |
| | 93 | | } |
| | 94 | | else |
| | 95 | | { |
| 1 | 96 | | keep.Add(task, null); |
| | 97 | | } |
| | 98 | | } |
| 1 | 99 | | _requests = keep; |
| 1 | 100 | | _instance = null; |
| 1 | 101 | | while (_requests.Count > 0) |
| | 102 | | { |
| 1 | 103 | | System.Threading.Monitor.Wait(_mutex); |
| | 104 | | } |
| 1 | 105 | | } |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | public void remove(RetryTask task) |
| | 109 | | { |
| 1 | 110 | | lock (_mutex) |
| | 111 | | { |
| 1 | 112 | | if (_requests.Remove(task)) |
| | 113 | | { |
| 1 | 114 | | if (_instance == null && _requests.Count == 0) |
| | 115 | | { |
| | 116 | | // If we are destroying the queue, destroy is probably waiting on the queue to be empty. |
| 1 | 117 | | System.Threading.Monitor.Pulse(_mutex); |
| | 118 | | } |
| | 119 | | } |
| 1 | 120 | | } |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | public bool cancel(RetryTask task) |
| | 124 | | { |
| 1 | 125 | | lock (_mutex) |
| | 126 | | { |
| 1 | 127 | | if (_requests.Remove(task)) |
| | 128 | | { |
| 1 | 129 | | if (_instance != null) |
| | 130 | | { |
| 1 | 131 | | return _instance.timer().cancel(task); |
| | 132 | | } |
| 0 | 133 | | else if (_requests.Count == 0) |
| | 134 | | { |
| | 135 | | // If we are destroying the queue, destroy is probably waiting on the queue to be empty. |
| 0 | 136 | | System.Threading.Monitor.Pulse(_mutex); |
| | 137 | | } |
| | 138 | | } |
| 0 | 139 | | return false; |
| | 140 | | } |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | private Instance _instance; |
| 1 | 144 | | private Dictionary<RetryTask, object> _requests = new(); |
| 1 | 145 | | private readonly object _mutex = new(); |
| | 146 | | } |