Havret commented on a change in pull request #18:
URL:
https://github.com/apache/activemq-nms-openwire/pull/18#discussion_r829480693
##########
File path: src/Transport/FutureResponse.cs
##########
@@ -15,77 +15,23 @@
* limitations under the License.
*/
-using System;
-using System.Threading;
+using System.Threading.Tasks;
using Apache.NMS.ActiveMQ.Commands;
-using Apache.NMS.Util;
namespace Apache.NMS.ActiveMQ.Transport
{
/// <summary>
/// Handles asynchronous responses
/// </summary>
- public class FutureResponse
+ public class FutureResponse : TaskCompletionSource<Response>
{
- private TimeSpan maxWait =
TimeSpan.FromMilliseconds(Timeout.Infinite);
- public TimeSpan ResponseTimeout
+ public FutureResponse():
base(TaskCreationOptions.RunContinuationsAsynchronously)
{
- get { return maxWait; }
- set { maxWait = value; }
}
-
- private readonly CountDownLatch latch = new CountDownLatch(1);
- private Response response;
-
+
public Response Response
{
- // Blocks the caller until a value has been set
- get
- {
- lock(latch)
- {
- if(null != response)
- {
- return response;
- }
- }
-
- try
- {
- if(!latch.await(maxWait) && response ==
null)
- {
- throw new
RequestTimedOutException(maxWait);
- }
- }
- catch(RequestTimedOutException e)
- {
- Tracer.Error("Caught Timeout Exception
while waiting on monitor: " + e);
- throw;
- }
- catch(Exception e)
- {
- Tracer.Error("Caught Exception while
waiting on monitor: " + e);
- }
-
- if(response == null &&
maxWait.TotalMilliseconds > 0)
- {
- }
-
- lock(latch)
- {
- return response;
- }
- }
-
- set
- {
- lock(latch)
- {
- response = value;
- }
-
- latch.countDown();
- }
+ set => SetResult(value);
Review comment:
This is not safe, it will throw an exception when you call `SetCanceled`
or `SetException` first. I'd change it here and in other places to Try*
equivalent.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]