private void SendMessages() { if (_sending || _messages == null || _messages.Count == 0) { return; } Task.Factory.StartNew( async(obj) => { if (_sending || _messages == null || _messages.Count == 0) { return; } _sending = true; #if !NET40 await Task.Delay(10).ConfigureAwait(false); #endif byte[] msgs = null; lock (_messageLock) { if (_messages != null && _messages.Count > 0) { msgs = BackplaneMessage.Serialize(_messages.ToArray()); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("Backplane is sending {0} messages ({1} skipped).", _messages.Count, _skippedMessages); } Interlocked.Add(ref MessagesSent, _messages.Count); _skippedMessages = 0; _messages.Clear(); } try { if (msgs != null) { Publish(msgs); Interlocked.Increment(ref SentChunks); } } catch (Exception ex) { _logger.LogError(ex, "Error occurred sending backplane messages."); } _sending = false; } #if NET40 }, this, _source.Token, TaskCreationOptions.None, TaskScheduler.Default) .ConfigureAwait(false); #else },
public void BackplaneMessage_Mulitple_IgnoreOwner() { // arrange var owner = new byte[] { 1, 2, 3, 4 }; var messages = CreateMany(owner); var serialized = BackplaneMessage.Serialize(messages.ToArray()); var deserialized = BackplaneMessage.Deserialize(serialized, skipOwner: owner).ToArray(); messages.Count.Should().Be(41); deserialized.Length.Should().Be(0); }
public void BackplaneMessage_Mulitple() { // arrange var owner = new byte[] { 1, 2, 3, 4 }; var messages = CreateMany(owner); var serialized = BackplaneMessage.Serialize(messages.ToArray()); var deserialized = BackplaneMessage.Deserialize(serialized).ToArray(); messages.Count.Should().Be(41); deserialized.ShouldAllBeEquivalentTo(messages); }
public void BackplaneMessage_Clear() { // arrange var owner = new byte[] { 1, 2, 3, 4 }; var msg = BackplaneMessage.ForClear(owner); // act var serialized = BackplaneMessage.Serialize(msg); var deserialized = BackplaneMessage.Deserialize(serialized).First(); // assert deserialized.Key.Should().Be(null); deserialized.Region.Should().Be(null); deserialized.ChangeAction.Should().Be(CacheItemChangedEventAction.Invalid); deserialized.Action.Should().Be(BackplaneAction.Clear); deserialized.OwnerIdentity.ShouldBeEquivalentTo(owner); deserialized.ShouldBeEquivalentTo(msg); }
public void BackplaneMessage_ChangePutKey() { // arrange var owner = new byte[] { 1, 2, 3, 4 }; var key = Guid.NewGuid().ToString(); var msg = BackplaneMessage.ForChanged(owner, key, CacheItemChangedEventAction.Put); // act var serialized = BackplaneMessage.Serialize(msg); var deserialized = BackplaneMessage.Deserialize(serialized).First(); // assert deserialized.Key.Should().Be(key); deserialized.Region.Should().Be(null); deserialized.ChangeAction.Should().Be(CacheItemChangedEventAction.Put); deserialized.Action.Should().Be(BackplaneAction.Changed); deserialized.OwnerIdentity.ShouldBeEquivalentTo(owner); deserialized.ShouldBeEquivalentTo(msg); }
private void PublishMessage(BackplaneMessage message) { var msg = message.Serialize(); lock (this.messageLock) { if (message.Action == BackplaneAction.Clear) { Interlocked.Exchange(ref this.skippedMessages, this.messages.Count); this.messages.Clear(); } if (!this.messages.Add(msg)) { Interlocked.Increment(ref this.skippedMessages); if (this.logger.IsEnabled(LogLevel.Trace)) { this.logger.LogTrace("Skipped duplicate message: {0}.", msg); } } this.SendMessages(); } }
private void SendMessages(object state) { if (_sending || _messages == null || _messages.Count == 0) { return; } Task.Factory.StartNew( async(obj) => { if (_sending || _messages == null || _messages.Count == 0) { return; } _sending = true; if (state != null && state is bool boolState && boolState == true) { _logger.LogInfo($"Backplane is sending {_messages.Count} messages triggered by timer."); } #if !NET40 await Task.Delay(10).ConfigureAwait(false); #endif byte[] msgs = null; lock (_messageSendLock) { if (_messages != null && _messages.Count > 0) { msgs = BackplaneMessage.Serialize(_messages.ToArray()); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("Backplane is sending {0} messages ({1} skipped).", _messages.Count, _skippedMessages); } try { if (msgs != null) { Publish(msgs); Interlocked.Increment(ref SentChunks); Interlocked.Add(ref MessagesSent, _messages.Count); _skippedMessages = 0; // clearing up only after successfully sending. Basically retrying... _messages.Clear(); // reset log limmiter because we just send stuff loggedLimitWarningOnce = false; } } catch (Exception ex) { _logger.LogError(ex, "Error occurred sending backplane messages."); } } _sending = false; } #if NET40 }, this, _source.Token, TaskCreationOptions.None, TaskScheduler.Default) ; #else },
public void SerializeClear() { var fullMessage = BackplaneMessage.Serialize(_dataSingleClear); }
public void SerializeChangeRegion() { var fullMessage = BackplaneMessage.Serialize(_dataSingleChangeRegion); }
public void SerializeMany() { var bytes = BackplaneMessage.Serialize(_multiple); }
public void SerializeRemove() { var fullMessage = BackplaneMessage.Serialize(_dataSingleRemove); }
private async Task SendMessagesAsync(object state) { if (_sending || _messages == null || _messages.Count == 0) { return; } if (_sending || _messages == null || _messages.Count == 0) { return; } _sending = true; if (state != null && state is bool boolState && boolState == true) { _logger.LogInfo($"Backplane is sending {_messages.Count} messages triggered by timer."); } #if !NET40 await Task.Delay(10).ConfigureAwait(false); #endif byte[] msgs = null; await _messageSendAsyncLock.WaitAsync(TimeSpan.FromSeconds(10)).ConfigureAwait(false); try { if (_messages != null && _messages.Count > 0) { msgs = BackplaneMessage.Serialize(_messages.ToArray()); if (_logger.IsEnabled(LogLevel.Debug)) { _logger.LogDebug("Backplane is sending {0} messages ({1} skipped).", _messages.Count, _skippedMessages); } try { if (msgs != null) { await PublishAsync(msgs).ConfigureAwait(false); Interlocked.Increment(ref SentChunks); Interlocked.Add(ref MessagesSent, _messages.Count); _skippedMessages = 0; // clearing up only after successfully sending. Basically retrying... _messages.Clear(); // reset log limmiter because we just send stuff loggedLimitWarningOnce = false; } } catch (Exception ex) { _logger.LogError(ex, "Error occurred sending backplane messages."); } } _sending = false; } finally { _messageSendAsyncLock.Release(); } }
private void PublishMessage(BackplaneMessage message) { byte[] serializedMessage = BackplaneMessage.Serialize(message); _publishSubscribe.Publish(_channelName, serializedMessage); }