private IMessage GetSourceMessage(IMessage message, List <Attempt> attempts) { var badMessageCurrentLevel = TopicNameHelpers.ExtractBadMessageLevel(message.Topic); while (message != null && badMessageCurrentLevel >= 0) { var badMessageContents = JsonConvert.DeserializeObject <BadMessageContents>( Encoding.UTF8.GetString(message.MessageBytes)); var attemp = new Attempt() { BadMessageContents = badMessageContents, MessageAddress = message.MessageAddress, Topic = message.Topic, Timestamp = message.Timestamp }; attempts.Insert(0, attemp); // retrying var messageTimestamp = message.Timestamp; var messageAgeMilliseconds = (int)Math.Round((DateTimeOffset.Now - messageTimestamp).TotalMilliseconds); var delayMilliseconds = configuration.LevelDelaysInSeconds[badMessageCurrentLevel] * 1000; if (messageAgeMilliseconds >= delayMilliseconds) { // the message is being replaced here with the original message message = pubSub.ReadSingle(badMessageContents.SourceMessageAddress); badMessageCurrentLevel = TopicNameHelpers.ExtractBadMessageLevel(message.Topic); } else { var reloadAt = DateTimeOffset.Now.AddMilliseconds(delayMilliseconds - messageAgeMilliseconds); logger.LogDebug("Retrying message but delay time did not ellapse yet, so ignoring it for now. Will reload PubSub at {ReloadAt}", reloadAt); Subscription?.ReloadAt(reloadAt); message = null; // retry time not ellapsed } } return(message); }