<commit_before>package no.stelar7.api.l4j8.basic.ratelimiting;


import no.stelar7.api.l4j8.basic.DataCall;

import java.time.Instant;
import java.util.*;
import java.util.Map.Entry;

/**
 * Burst ratelimiter will use as many calls as possible, then wait when it reaches the limit
 */
public class BurstRateLimiter extends RateLimiter
{
    
    public BurstRateLimiter(RateLimit... limits)
    {
        super(limits);
    }
    
    @Override
    public void acquire()
    {
        try
        {
            semaphore.acquireUninterruptibly();
            
            Thread.sleep(getDelay());
            
            update();
            
            semaphore.release();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public void updatePermitsPerX(Map<Integer, Long> data)
    {
        semaphore.acquireUninterruptibly();
        
        for (Entry<Integer, Long> key : data.entrySet())
        {
            limits.stream().filter(l -> l.getTimeframeInMS() / 1000 == key.getKey()).forEach(l ->
                                                                                             {
                                                                                                 long oldVal = callCountInTime.get(l);
                                                                                                 long newVal = key.getValue();
                                                                                                 if (oldVal + 1 < newVal)
                                                                                                 {
                                                                                                     callCountInTime.put(l, newVal);
                    
                                                                                                     if (DataCall.VERBOSE_LIMITING)
                                                                                                     {
                                                                                                         System.out.println("limit " + key + " has changed from " + oldVal + " to " + newVal);
                                                                                                     }
                                                                                                 }
                                                                                             });
        }
        semaphore.release();
    }
    
    private long getDelay()
    {
        Instant now   = Instant.now();
        long[]  delay = {0};
        int     bias  = 2;
        
        limits.stream().sorted(Comparator.comparing(RateLimit::getTimeframeInMS)).forEachOrdered(limit ->
                                                                                                 {
                                                                                                     if (callCountInTime.get(limit) >= limit.getRequests())
                                                                                                     {
                                                                                                         long newDelay = firstCallInTime.get(limit).toEpochMilli() + limit.getTimeframeInMS() - now.toEpochMilli();
                                                                                                         if (newDelay > delay[0])
                                                                                                         {
                                                                                                             delay[0] = newDelay;
                                                                                                         }
                                                                                                     }
                                                                                                 });
        
        if (delay[0] != 0)
        {
            delay[0] = (long) ((Math.ceil(delay[0] / 1000f) + bias) * 1000L);
        }
        
        if (DataCall.VERBOSE_LIMITING)
        {
            System.out.println("Sleeping for: " + delay[0]);
        }
        
        return delay[0];
    }
    
    private void update()
    {
        Instant now = Instant.now();
        limits.forEach(l ->
                       {
                           if (firstCallInTime.get(l).toEpochMilli() < now.toEpochMilli() - l.getTimeframeInMS())
                           {
                               firstCallInTime.put(l, now);
                               callCountInTime.put(l, 0L);
                           }
            
                           callCountInTime.compute(l, (k, v) -> v + 1);
            
                           if (DataCall.VERBOSE_LIMITING)
                           {
                               System.out.println("Calls made: " + callCountInTime.get(l) + " in: " + l.getTimeframeInMS() / 1000);
                           }
                       });
    }
    
}<commit_msg>Update before sleeping<commit_after>package no.stelar7.api.l4j8.basic.ratelimiting;


import no.stelar7.api.l4j8.basic.DataCall;

import java.time.Instant;
import java.util.*;
import java.util.Map.Entry;

/**
 * Burst ratelimiter will use as many calls as possible, then wait when it reaches the limit
 */
public class BurstRateLimiter extends RateLimiter
{
    
    public BurstRateLimiter(RateLimit... limits)
    {
        super(limits);
    }
    
    @Override
    public void acquire()
    {
        try
        {
            semaphore.acquireUninterruptibly();
            
            update();
            
            Thread.sleep(getDelay());
            
            semaphore.release();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    
    @Override
    public void updatePermitsPerX(Map<Integer, Long> data)
    {
        semaphore.acquireUninterruptibly();
        
        for (Entry<Integer, Long> key : data.entrySet())
        {
            limits.stream().filter(l -> l.getTimeframeInMS() / 1000 == key.getKey()).forEach(l ->
                                                                                             {
                                                                                                 long oldVal = callCountInTime.get(l);
                                                                                                 long newVal = key.getValue();
                                                                                                 if (oldVal + 1 < newVal)
                                                                                                 {
                                                                                                     callCountInTime.put(l, newVal);
                    
                                                                                                     if (DataCall.VERBOSE_LIMITING)
                                                                                                     {
                                                                                                         System.out.println("limit " + key + " has changed from " + oldVal + " to " + newVal);
                                                                                                     }
                                                                                                 }
                                                                                             });
        }
        semaphore.release();
    }
    
    private long getDelay()
    {
        Instant now   = Instant.now();
        long[]  delay = {0};
        int     bias  = 2;
        
        limits.stream().sorted(Comparator.comparing(RateLimit::getTimeframeInMS)).forEachOrdered(limit ->
                                                                                                 {
                                                                                                     if (callCountInTime.get(limit) >= limit.getRequests())
                                                                                                     {
                                                                                                         long newDelay = firstCallInTime.get(limit).toEpochMilli() + limit.getTimeframeInMS() - now.toEpochMilli();
                                                                                                         if (newDelay > delay[0])
                                                                                                         {
                                                                                                             delay[0] = newDelay;
                                                                                                         }
                                                                                                     }
                                                                                                 });
        
        if (delay[0] != 0)
        {
            delay[0] = (long) ((Math.ceil(delay[0] / 1000f) + bias) * 1000L);
        }
        
        if (DataCall.VERBOSE_LIMITING)
        {
            System.out.println("Sleeping for: " + delay[0]);
        }
        
        return delay[0];
    }
    
    private void update()
    {
        Instant now = Instant.now();
        limits.forEach(limit ->
                       {
                           if ((firstCallInTime.get(limit).toEpochMilli() - now.toEpochMilli()) + limit.getTimeframeInMS() < 0)
                           {
                               firstCallInTime.put(limit, now);
                               callCountInTime.put(limit, 0L);
                           }
            
                           callCountInTime.compute(limit, (k, v) -> v + 1);
            
                           if (DataCall.VERBOSE_LIMITING)
                           {
                               System.out.println("Calls made: " + callCountInTime.get(limit) + " in: " + limit.getTimeframeInMS() / 1000);
                           }
                       });
    }
    
}
{'fraction_non_alphanumeric': 0.07172268505218807, 'fraction_numerical': 0.006798812601742794, 'mean_word_length': 0.5383708940933863, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 184, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '141535', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1840, 'n_words': 317}