<commit_before>﻿using System;
using JustSaying.Models;

namespace JustSaying.Messaging.MessageHandling
{
    public class ExactlyOnceHandler<T> : IHandler<T> where T : Message
    {
        private readonly IHandler<T> _inner;
        private readonly IMessageLock _messageLock;
        private const int TEMPORARY_LOCK_SECONDS = 15;

        public ExactlyOnceHandler(IHandler<T>  inner, IMessageLock messageLock)
        {
            _inner = inner;
            _messageLock = messageLock;
        }

        public bool Handle(T message)
        {
            var lockKey = message.Id.ToString();
            bool canLock = _messageLock.TryAquire(lockKey, TimeSpan.FromSeconds(TEMPORARY_LOCK_SECONDS));
            if (!canLock)
                return true;

            try
            {
                var successfullyHandled = _inner.Handle(message);
                if (successfullyHandled)
                {
                    _messageLock.TryAquire(lockKey);
                }
                return successfullyHandled;
            }
            catch
            {
                _messageLock.Release(lockKey);
                throw;
            }
        }
    }
}
<commit_msg>Undo the temporary hack I added and use message.UniqueKey() instead of Id as the equality check<commit_after>﻿using System;
using JustSaying.Models;

namespace JustSaying.Messaging.MessageHandling
{
    public class ExactlyOnceHandler<T> : IHandler<T> where T : Message
    {
        private readonly IHandler<T> _inner;
        private readonly IMessageLock _messageLock;
        private const int TEMPORARY_LOCK_SECONDS = 15;

        public ExactlyOnceHandler(IHandler<T>  inner, IMessageLock messageLock)
        {
            _inner = inner;
            _messageLock = messageLock;
        }

        public bool Handle(T message)
        {
            var lockKey = message.UniqueKey();
            bool canLock = _messageLock.TryAquire(lockKey, TimeSpan.FromSeconds(TEMPORARY_LOCK_SECONDS));
            if (!canLock)
                return true;

            try
            {
                var successfullyHandled = _inner.Handle(message);
                if (successfullyHandled)
                {
                    _messageLock.TryAquire(lockKey);
                }
                return successfullyHandled;
            }
            catch
            {
                _messageLock.Release(lockKey);
                throw;
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.06731549067315491, 'fraction_numerical': 0.0016220600162206002, 'mean_word_length': 1.618895966029724, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4737589', 'n_tokens_mistral': 662, 'n_tokens_neox': 625, 'n_words': 139}