/// <summary> /// Add a new query to the queue. /// Return true if successful /// </summary> public bool Enqueue(JObject query) { if (Lock || WaitLoginToReplay) { return(false); } queueSemaphore.Wait(); try { if (queue.Count < offlineManager.MaxQueueSize || offlineManager.MaxQueueSize < 0) { if (queue.Count == 0) { stopWatch.Reset(); stopWatch.Start(); queue.Add(new TimedQuery(query, 0)); } else { TimedQuery previous = queue[queue.Count - 1]; Int64 elapsedTime = stopWatch.ElapsedMilliseconds - previous.Time; elapsedTime = Math.Min(elapsedTime, offlineManager.MaxRequestDelay); queue.Add(new TimedQuery(query, previous.Time + elapsedTime)); } String controller = query["controller"]?.ToString(); String action = query["action"]?.ToString(); if (controller == "auth" && (action == "login" || action == "logout")) { Lock = true; } return(true); } } finally { queueSemaphore.Release(); } return(false); }
/// <summary> /// Replay one query. /// </summary> internal Task ReplayOneQuery(TimedQuery timedQuery, CancellationToken cancellationToken) { if (offlineManager.MaxRequestDelay == 0) { if (offlineManager.QueueFilter(timedQuery.Query)) { offlineManager.NetworkProtocol.Send(timedQuery.Query); } return(null); } return(Task.Run(async() => { if (offlineManager.QueueFilter(timedQuery.Query)) { cancellationToken.ThrowIfCancellationRequested(); await Task.Delay((Int32)timedQuery.Time, cancellationToken); cancellationToken.ThrowIfCancellationRequested(); timedQuery.Query["jwt"] = kuzzle.AuthenticationToken; offlineManager.NetworkProtocol.Send(timedQuery.Query); } }, cancellationToken)); }