Lean  $LEAN_TAG$
BrokerageConcurrentMessageHandler.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System;
17 using System.Threading;
18 using QuantConnect.Logging;
19 using System.Collections.Generic;
20 
22 {
23  /// <summary>
24  /// Brokerage helper class to lock message stream while executing an action, for example placing an order
25  /// </summary>
26  public class BrokerageConcurrentMessageHandler<T> where T : class
27  {
28  private readonly Action<T> _processMessages;
29  private readonly Queue<T> _messageBuffer;
30  private readonly object _streamLocked;
31 
32  /// <summary>
33  /// Creates a new instance
34  /// </summary>
35  /// <param name="processMessages">The action to call for each new message</param>
36  public BrokerageConcurrentMessageHandler(Action<T> processMessages)
37  {
38  _processMessages = processMessages;
39  _messageBuffer = new Queue<T>();
40  _streamLocked = new object();
41  }
42 
43  /// <summary>
44  /// Will process or enqueue a message for later processing it
45  /// </summary>
46  /// <param name="message">The new message</param>
47  public void HandleNewMessage(T message)
48  {
49  lock (_messageBuffer)
50  {
51  if (Monitor.TryEnter(_streamLocked))
52  {
53  try
54  {
55  ProcessMessages(message);
56  }
57  finally
58  {
59  Monitor.Exit(_streamLocked);
60  }
61  }
62  else
63  {
64  if (message != default)
65  {
66  // if someone has the lock just enqueue the new message they will process any remaining messages
67  // if by chance they are about to free the lock, no worries, we will always process first any remaining message first see 'ProcessMessages'
68  _messageBuffer.Enqueue(message);
69  }
70  }
71  }
72  }
73 
74  /// <summary>
75  /// Lock the streaming processing while we're sending orders as sometimes they fill before the call returns.
76  /// </summary>
77  public void WithLockedStream(Action code)
78  {
79  Monitor.Enter(_streamLocked);
80  try
81  {
82  code();
83  }
84  finally
85  {
86  // once we finish our 'code' we will process any message that come through,
87  // to make sure no message get's left behind (race condition between us finishing 'ProcessMessages'
88  // and some message being enqueued to it, we just take a lock on the buffer
89  lock (_messageBuffer)
90  {
91  // we release the '_streamLocked' first so by the time we release '_messageBuffer' any new message is processed immediately and not enqueued
92  Monitor.Exit(_streamLocked);
93  ProcessMessages();
94  }
95  }
96  }
97 
98  /// <summary>
99  /// Process any pending message and the provided one if any
100  /// </summary>
101  /// <remarks>To be called owing the stream lock</remarks>
102  private void ProcessMessages(T message = null)
103  {
104  try
105  {
106  // double check there isn't any pending message
107  while (_messageBuffer.TryDequeue(out var e))
108  {
109  _processMessages(e);
110  }
111 
112  if (message != null)
113  {
114  _processMessages(message);
115  }
116  }
117  catch (Exception e)
118  {
119  Log.Error(e);
120  }
121  }
122  }
123 }