|
@@ -2,6 +2,7 @@ |
|
|
// Licensed under the MIT License. See License.txt in the project root for license information. |
|
|
// Licensed under the MIT License. See License.txt in the project root for license information. |
|
|
|
|
|
|
|
|
using System; |
|
|
using System; |
|
|
|
|
|
using System.Collections.Concurrent; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
using System.Threading; |
|
|
using System.Threading; |
|
@@ -24,19 +25,19 @@ namespace DotNetCore.CAP.InMemoryStorage |
|
|
_capOptions = capOptions; |
|
|
_capOptions = capOptions; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static IList<MemoryMessage> PublishedMessages { get; } = new List<MemoryMessage>(); |
|
|
|
|
|
|
|
|
public static ConcurrentDictionary<string, MemoryMessage> PublishedMessages { get; } = new ConcurrentDictionary<string, MemoryMessage>(); |
|
|
|
|
|
|
|
|
public static IList<MemoryMessage> ReceivedMessages { get; } = new List<MemoryMessage>(); |
|
|
|
|
|
|
|
|
public static ConcurrentDictionary<string, MemoryMessage> ReceivedMessages { get; } = new ConcurrentDictionary<string, MemoryMessage>(); |
|
|
|
|
|
|
|
|
public Task ChangePublishStateAsync(MediumMessage message, StatusName state) |
|
|
public Task ChangePublishStateAsync(MediumMessage message, StatusName state) |
|
|
{ |
|
|
{ |
|
|
PublishedMessages.First(x => x.DbId == message.DbId).StatusName = state; |
|
|
|
|
|
|
|
|
PublishedMessages[message.DbId].StatusName = state; |
|
|
return Task.CompletedTask; |
|
|
return Task.CompletedTask; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Task ChangeReceiveStateAsync(MediumMessage message, StatusName state) |
|
|
public Task ChangeReceiveStateAsync(MediumMessage message, StatusName state) |
|
|
{ |
|
|
{ |
|
|
ReceivedMessages.First(x => x.DbId == message.DbId).StatusName = state; |
|
|
|
|
|
|
|
|
ReceivedMessages[message.DbId].StatusName = state; |
|
|
return Task.CompletedTask; |
|
|
return Task.CompletedTask; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@@ -52,7 +53,7 @@ namespace DotNetCore.CAP.InMemoryStorage |
|
|
Retries = 0 |
|
|
Retries = 0 |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
PublishedMessages.Add(new MemoryMessage() |
|
|
|
|
|
|
|
|
PublishedMessages[message.DbId] = new MemoryMessage() |
|
|
{ |
|
|
{ |
|
|
DbId = message.DbId, |
|
|
DbId = message.DbId, |
|
|
Name = name, |
|
|
Name = name, |
|
@@ -61,24 +62,27 @@ namespace DotNetCore.CAP.InMemoryStorage |
|
|
Added = message.Added, |
|
|
Added = message.Added, |
|
|
ExpiresAt = message.ExpiresAt, |
|
|
ExpiresAt = message.ExpiresAt, |
|
|
StatusName = StatusName.Scheduled |
|
|
StatusName = StatusName.Scheduled |
|
|
}); |
|
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
return message; |
|
|
return message; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void StoreReceivedExceptionMessage(string name, string group, string content) |
|
|
public void StoreReceivedExceptionMessage(string name, string group, string content) |
|
|
{ |
|
|
{ |
|
|
ReceivedMessages.Add(new MemoryMessage |
|
|
|
|
|
|
|
|
var id = SnowflakeId.Default().NextId().ToString(); |
|
|
|
|
|
|
|
|
|
|
|
ReceivedMessages[id] = new MemoryMessage |
|
|
{ |
|
|
{ |
|
|
DbId = SnowflakeId.Default().NextId().ToString(), |
|
|
|
|
|
|
|
|
DbId = id, |
|
|
Group = group, |
|
|
Group = group, |
|
|
|
|
|
Origin = null, |
|
|
Name = name, |
|
|
Name = name, |
|
|
Content = content, |
|
|
Content = content, |
|
|
Retries = _capOptions.Value.FailedRetryCount, |
|
|
Retries = _capOptions.Value.FailedRetryCount, |
|
|
Added = DateTime.Now, |
|
|
Added = DateTime.Now, |
|
|
ExpiresAt = DateTime.Now.AddDays(15), |
|
|
ExpiresAt = DateTime.Now.AddDays(15), |
|
|
StatusName = StatusName.Failed |
|
|
StatusName = StatusName.Failed |
|
|
}); |
|
|
|
|
|
|
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public MediumMessage StoreReceivedMessage(string name, string @group, Message message) |
|
|
public MediumMessage StoreReceivedMessage(string name, string @group, Message message) |
|
@@ -92,9 +96,10 @@ namespace DotNetCore.CAP.InMemoryStorage |
|
|
Retries = 0 |
|
|
Retries = 0 |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ReceivedMessages.Add(new MemoryMessage |
|
|
|
|
|
|
|
|
ReceivedMessages[mdMessage.DbId] = new MemoryMessage |
|
|
{ |
|
|
{ |
|
|
DbId = mdMessage.DbId, |
|
|
DbId = mdMessage.DbId, |
|
|
|
|
|
Origin = mdMessage.Origin, |
|
|
Group = group, |
|
|
Group = group, |
|
|
Name = name, |
|
|
Name = name, |
|
|
Content = StringSerializer.Serialize(mdMessage.Origin), |
|
|
Content = StringSerializer.Serialize(mdMessage.Origin), |
|
@@ -102,38 +107,69 @@ namespace DotNetCore.CAP.InMemoryStorage |
|
|
Added = mdMessage.Added, |
|
|
Added = mdMessage.Added, |
|
|
ExpiresAt = mdMessage.ExpiresAt, |
|
|
ExpiresAt = mdMessage.ExpiresAt, |
|
|
StatusName = StatusName.Failed |
|
|
StatusName = StatusName.Failed |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}; |
|
|
return mdMessage; |
|
|
return mdMessage; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Task<int> DeleteExpiresAsync(string table, DateTime timeout, int batchCount = 1000, CancellationToken token = default) |
|
|
public Task<int> DeleteExpiresAsync(string table, DateTime timeout, int batchCount = 1000, CancellationToken token = default) |
|
|
{ |
|
|
{ |
|
|
var ret = table == nameof(PublishedMessages) |
|
|
|
|
|
? ((List<MemoryMessage>)PublishedMessages).RemoveAll(x => x.ExpiresAt < timeout) |
|
|
|
|
|
: ((List<MemoryMessage>)ReceivedMessages).RemoveAll(x => x.ExpiresAt < timeout); |
|
|
|
|
|
return Task.FromResult(ret); |
|
|
|
|
|
|
|
|
var removed = 0; |
|
|
|
|
|
if (table == nameof(PublishedMessages)) |
|
|
|
|
|
{ |
|
|
|
|
|
var ids = PublishedMessages.Values.Where(x => x.ExpiresAt < timeout).Select(x => x.DbId).ToList(); |
|
|
|
|
|
foreach (var id in ids) |
|
|
|
|
|
{ |
|
|
|
|
|
if (PublishedMessages.TryRemove(id, out _)) |
|
|
|
|
|
{ |
|
|
|
|
|
removed++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
var ids = ReceivedMessages.Values.Where(x => x.ExpiresAt < timeout).Select(x => x.DbId).ToList(); |
|
|
|
|
|
foreach (var id in ids) |
|
|
|
|
|
{ |
|
|
|
|
|
if (PublishedMessages.TryRemove(id, out _)) |
|
|
|
|
|
{ |
|
|
|
|
|
removed++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return Task.FromResult(removed); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Task<IEnumerable<MediumMessage>> GetPublishedMessagesOfNeedRetry() |
|
|
public Task<IEnumerable<MediumMessage>> GetPublishedMessagesOfNeedRetry() |
|
|
{ |
|
|
{ |
|
|
var ret = PublishedMessages |
|
|
|
|
|
|
|
|
var ret = PublishedMessages.Values |
|
|
.Where(x => x.Retries < _capOptions.Value.FailedRetryCount |
|
|
.Where(x => x.Retries < _capOptions.Value.FailedRetryCount |
|
|
&& x.Added < DateTime.Now.AddSeconds(-10) |
|
|
&& x.Added < DateTime.Now.AddSeconds(-10) |
|
|
&& (x.StatusName == StatusName.Scheduled || x.StatusName == StatusName.Failed)) |
|
|
&& (x.StatusName == StatusName.Scheduled || x.StatusName == StatusName.Failed)) |
|
|
.Take(200) |
|
|
.Take(200) |
|
|
.Select(x => (MediumMessage)x); |
|
|
.Select(x => (MediumMessage)x); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var message in ret) |
|
|
|
|
|
{ |
|
|
|
|
|
message.Origin = StringSerializer.DeSerialize(message.Content); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return Task.FromResult(ret); |
|
|
return Task.FromResult(ret); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Task<IEnumerable<MediumMessage>> GetReceivedMessagesOfNeedRetry() |
|
|
public Task<IEnumerable<MediumMessage>> GetReceivedMessagesOfNeedRetry() |
|
|
{ |
|
|
{ |
|
|
var ret = ReceivedMessages |
|
|
|
|
|
|
|
|
var ret = ReceivedMessages.Values |
|
|
.Where(x => x.Retries < _capOptions.Value.FailedRetryCount |
|
|
.Where(x => x.Retries < _capOptions.Value.FailedRetryCount |
|
|
&& x.Added < DateTime.Now.AddSeconds(-10) |
|
|
&& x.Added < DateTime.Now.AddSeconds(-10) |
|
|
&& (x.StatusName == StatusName.Scheduled || x.StatusName == StatusName.Failed)) |
|
|
&& (x.StatusName == StatusName.Scheduled || x.StatusName == StatusName.Failed)) |
|
|
.Take(200) |
|
|
.Take(200) |
|
|
.Select(x => (MediumMessage)x); |
|
|
.Select(x => (MediumMessage)x); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var message in ret) |
|
|
|
|
|
{ |
|
|
|
|
|
message.Origin = StringSerializer.DeSerialize(message.Content); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return Task.FromResult(ret); |
|
|
return Task.FromResult(ret); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|