Made MessageQueue deletes less aggressive, allows editing of messages before they are sent.
This commit is contained in:
@@ -30,11 +30,11 @@ namespace Discord.Net
|
|||||||
private readonly Random _nonceRand;
|
private readonly Random _nonceRand;
|
||||||
private readonly RestClient _rest;
|
private readonly RestClient _rest;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
private readonly ConcurrentQueue<int> _pendingSends;
|
private readonly ConcurrentQueue<Message> _pendingSends;
|
||||||
private readonly ConcurrentQueue<MessageEdit> _pendingEdits;
|
private readonly ConcurrentQueue<MessageEdit> _pendingEdits;
|
||||||
private readonly ConcurrentQueue<Message> _pendingDeletes;
|
private readonly ConcurrentQueue<Message> _pendingDeletes;
|
||||||
private readonly ConcurrentDictionary<int, Message> _pendingSendsByNonce;
|
private readonly ConcurrentDictionary<int, string> _pendingSendsByNonce;
|
||||||
private readonly ConcurrentQueue<Task> _pendingTasks;
|
//private readonly ConcurrentQueue<Task> _pendingTasks;
|
||||||
private int _nextWarning;
|
private int _nextWarning;
|
||||||
private int _count;
|
private int _count;
|
||||||
|
|
||||||
@@ -47,11 +47,11 @@ namespace Discord.Net
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
_nonceRand = new Random();
|
_nonceRand = new Random();
|
||||||
_pendingSends = new ConcurrentQueue<int>();
|
_pendingSends = new ConcurrentQueue<Message>();
|
||||||
_pendingEdits = new ConcurrentQueue<MessageEdit>();
|
_pendingEdits = new ConcurrentQueue<MessageEdit>();
|
||||||
_pendingDeletes = new ConcurrentQueue<Message>();
|
_pendingDeletes = new ConcurrentQueue<Message>();
|
||||||
_pendingSendsByNonce = new ConcurrentDictionary<int, Message>();
|
_pendingSendsByNonce = new ConcurrentDictionary<int, string>();
|
||||||
_pendingTasks = new ConcurrentQueue<Task>();
|
//_pendingTasks = new ConcurrentQueue<Task>();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Message QueueSend(Channel channel, string text, bool isTTS)
|
internal Message QueueSend(Channel channel, string text, bool isTTS)
|
||||||
@@ -60,11 +60,11 @@ namespace Discord.Net
|
|||||||
msg.RawText = text;
|
msg.RawText = text;
|
||||||
msg.Text = msg.Resolve(text);
|
msg.Text = msg.Resolve(text);
|
||||||
msg.Nonce = GenerateNonce();
|
msg.Nonce = GenerateNonce();
|
||||||
if (_pendingSendsByNonce.TryAdd(msg.Nonce, msg))
|
if (_pendingSendsByNonce.TryAdd(msg.Nonce, text))
|
||||||
{
|
{
|
||||||
msg.State = MessageState.Queued;
|
msg.State = MessageState.Queued;
|
||||||
IncrementCount();
|
IncrementCount();
|
||||||
_pendingSends.Enqueue(msg.Nonce);
|
_pendingSends.Enqueue(msg);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
msg.State = MessageState.Failed;
|
msg.State = MessageState.Failed;
|
||||||
@@ -72,12 +72,15 @@ namespace Discord.Net
|
|||||||
}
|
}
|
||||||
internal void QueueEdit(Message msg, string text)
|
internal void QueueEdit(Message msg, string text)
|
||||||
{
|
{
|
||||||
|
string msgText = msg.RawText;
|
||||||
|
if (msg.State == MessageState.Queued && _pendingSendsByNonce.TryUpdate(msg.Nonce, msgText, text))
|
||||||
|
return;
|
||||||
IncrementCount();
|
IncrementCount();
|
||||||
_pendingEdits.Enqueue(new MessageEdit(msg, text));
|
_pendingEdits.Enqueue(new MessageEdit(msg, text));
|
||||||
}
|
}
|
||||||
internal void QueueDelete(Message msg)
|
internal void QueueDelete(Message msg)
|
||||||
{
|
{
|
||||||
Message ignored;
|
string ignored;
|
||||||
if (msg.State == MessageState.Queued && _pendingSendsByNonce.TryRemove(msg.Nonce, out ignored))
|
if (msg.State == MessageState.Queued && _pendingSendsByNonce.TryRemove(msg.Nonce, out ignored))
|
||||||
{
|
{
|
||||||
//Successfully stopped the message from being sent in the first place
|
//Successfully stopped the message from being sent in the first place
|
||||||
@@ -95,7 +98,7 @@ namespace Discord.Net
|
|||||||
RunSendQueue(cancelToken),
|
RunSendQueue(cancelToken),
|
||||||
RunEditQueue(cancelToken),
|
RunEditQueue(cancelToken),
|
||||||
RunDeleteQueue(cancelToken),
|
RunDeleteQueue(cancelToken),
|
||||||
RunTaskQueue(cancelToken)
|
//RunTaskQueue(cancelToken)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
private Task RunSendQueue(CancellationToken cancelToken)
|
private Task RunSendQueue(CancellationToken cancelToken)
|
||||||
@@ -107,12 +110,16 @@ namespace Discord.Net
|
|||||||
{
|
{
|
||||||
while (!cancelToken.IsCancellationRequested)
|
while (!cancelToken.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
int nonce;
|
Message msg;
|
||||||
while (_pendingSends.TryDequeue(out nonce))
|
while (_pendingSends.TryDequeue(out msg))
|
||||||
{
|
{
|
||||||
Message msg;
|
string text;
|
||||||
if (_pendingSendsByNonce.TryRemove(nonce, out msg)) //If it was delete from queue, this will fail
|
if (_pendingSendsByNonce.TryRemove(msg.Nonce, out text)) //If it was delete from queue, this will fail
|
||||||
|
{
|
||||||
|
msg.RawText = text;
|
||||||
|
msg.Text = msg.Resolve(text);
|
||||||
await Send(msg).ConfigureAwait(false);
|
await Send(msg).ConfigureAwait(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay((int)Discord.DiscordConfig.MessageQueueInterval).ConfigureAwait(false);
|
await Task.Delay((int)Discord.DiscordConfig.MessageQueueInterval).ConfigureAwait(false);
|
||||||
@@ -135,7 +142,7 @@ namespace Discord.Net
|
|||||||
{
|
{
|
||||||
_pendingEdits.TryDequeue(out edit);
|
_pendingEdits.TryDequeue(out edit);
|
||||||
if (edit.Message.State == MessageState.Normal)
|
if (edit.Message.State == MessageState.Normal)
|
||||||
await Edit(edit.Message, edit.NewText);
|
await Edit(edit.Message, edit.NewText).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay((int)Discord.DiscordConfig.MessageQueueInterval).ConfigureAwait(false);
|
await Task.Delay((int)Discord.DiscordConfig.MessageQueueInterval).ConfigureAwait(false);
|
||||||
@@ -158,7 +165,7 @@ namespace Discord.Net
|
|||||||
{
|
{
|
||||||
_pendingDeletes.TryDequeue(out msg);
|
_pendingDeletes.TryDequeue(out msg);
|
||||||
if (msg.State == MessageState.Normal)
|
if (msg.State == MessageState.Normal)
|
||||||
_pendingTasks.Enqueue(Delete(msg));
|
await Delete(msg).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.Delay((int)Discord.DiscordConfig.MessageQueueInterval).ConfigureAwait(false);
|
await Task.Delay((int)Discord.DiscordConfig.MessageQueueInterval).ConfigureAwait(false);
|
||||||
@@ -167,7 +174,7 @@ namespace Discord.Net
|
|||||||
catch (OperationCanceledException) { }
|
catch (OperationCanceledException) { }
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
private Task RunTaskQueue(CancellationToken cancelToken)
|
/*private Task RunTaskQueue(CancellationToken cancelToken)
|
||||||
{
|
{
|
||||||
return Task.Run(async () =>
|
return Task.Run(async () =>
|
||||||
{
|
{
|
||||||
@@ -190,7 +197,7 @@ namespace Discord.Net
|
|||||||
await task.ConfigureAwait(false);
|
await task.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}*/
|
||||||
|
|
||||||
internal async Task Send(Message msg)
|
internal async Task Send(Message msg)
|
||||||
{
|
{
|
||||||
@@ -263,14 +270,13 @@ namespace Discord.Net
|
|||||||
/// <summary> Clears all queued message sends/edits/deletes. </summary>
|
/// <summary> Clears all queued message sends/edits/deletes. </summary>
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
int nonce;
|
|
||||||
while (_pendingSends.TryDequeue(out nonce)) { }
|
|
||||||
|
|
||||||
MessageEdit edit;
|
|
||||||
while (_pendingEdits.TryDequeue(out edit)) { }
|
|
||||||
|
|
||||||
Message msg;
|
Message msg;
|
||||||
|
MessageEdit edit;
|
||||||
|
|
||||||
|
while (_pendingSends.TryDequeue(out msg)) { }
|
||||||
|
while (_pendingEdits.TryDequeue(out edit)) { }
|
||||||
while (_pendingDeletes.TryDequeue(out msg)) { }
|
while (_pendingDeletes.TryDequeue(out msg)) { }
|
||||||
|
_pendingSendsByNonce.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int GenerateNonce()
|
private int GenerateNonce()
|
||||||
|
|||||||
Reference in New Issue
Block a user