public virtual PreemptiveRequest Request(int priority = 1, bool preempt = false)
        {
            var request = new PreemptiveRequest(Environment, TriggerRelease, DisposeCallback, priority, preempt);

            if (!RequestQueue.ContainsKey(request.Priority))
            {
                RequestQueue.Add(request.Priority, new LinkedList <PreemptiveRequest>());
            }
            RequestQueue[request.Priority].AddLast(request);
            TriggerRequest();
            return(request);
        }
        public virtual PriorityRequest Request(int priority = 1)
        {
            var request = new PriorityRequest(Environment, TriggerRelease, DisposeCallback, priority);

            if (!RequestQueue.ContainsKey(priority))
            {
                RequestQueue.Add(priority, new LinkedList <PriorityRequest>());
            }
            RequestQueue[priority].AddLast(request);
            TriggerRequest();
            return(request);
        }
예제 #3
0
        public async Task <T> RequestAsync <T>(string eventName, object data, int timeout)
        {
            if (_socket == null || _socket.disconnected)
            {
                throw new Exception("websocket not connected");
            }

            if (_requestQueue.Contains(eventName))
            {
                throw new Exception(string.Format("RequestQueue already contains request {0}", eventName));
            }

            _requestQueue.Add(eventName);
            _socket.emit(eventName, data);

            var timeoutSpan = timeout == 0
                ? TimeSpan.FromMilliseconds(DefaultRequestTimeout)
                : TimeSpan.FromMilliseconds(timeout);
            var timer = new Stopwatch();

            timer.Start();

            while (!_requestQueue.IsDone(eventName) && timer.Elapsed < timeoutSpan)
            {
                await Task.Delay(100);
            }

            var responseData = _requestQueue.Dequeue(eventName);

            timer.Stop();
            if (timer.Elapsed >= timeoutSpan)
            {
                throw new TimeoutException(string.Format("elapsed time = {0} millis", timer.Elapsed.TotalMilliseconds));
            }

            return(responseData.ToObject <T>());
        }
예제 #4
0
        public void Request()
        {
            var queue    = new RequestQueue();
            var expected = new RequestM
            {
                Type                = QueryType.Olap,
                IsSelect            = true,
                Query               = "SELECT * FROM my_table",
                AcceptRetries       = true,
                Priority            = 1,
                RequestId           = Guid.Parse("b2828949-6d7e-4d27-9f5e-b966fd829784"),
                IsRetried           = true,
                CurrentRetryAttempt = 2
            };

            queue.Add(expected);
            var actual = queue.Get();

            Assert.AreEqual(expected, actual);
        }
예제 #5
0
 public void EnqueueRequest(Request request)
 {
     RequestQueue.Add(request);
 }