【问题标题】:Guava RateLimiter BurstingGuava RateLimiter 爆裂
【发布时间】:2017-11-03 15:30:25
【问题描述】:

我一直在使用 thottling 系统,偶然发现了 Guava RateLimiter。据我所知,它处理节流的两种主要方式是对溢出的请求进行排队(.acquire(...) 方法)或丢弃它们(tryAcquire(...) 方法)

我在想会有一个选项允许请求达到指定数量,并且仅在达到所述限制队列或丢弃请求之后。

例如:

public static void main( String[] args )
{
    try
    {
        RateLimiter limiter = RateLimiter.create(5.0 );
        //fictive call not saying it should be implemented this way
        limiter.allowBursts(true);
        for( int i = 0; i < 20; i++ )
        {
            Thread.sleep( 100 );
            performOperation( limiter );
        }
    }
    catch( InterruptedException e )
    {
        e.printStackTrace();
    }
}

private static void performOperation( RateLimiter limiter )
{
    if( limiter.tryAcquire() )
    {
        System.out.println( Instant.now() + ": Beep" );
    }
}

这将打印出五声哔哔声,省略接下来的五声,然后再次打印五声

我是唯一一个认为这将是一个有用的功能的人,还是我没有抓住重点?

测试代码由: Throttling method calls using Guava RateLimiter class

【问题讨论】:

    标签: java guava throttling


    【解决方案1】:

    这将打印出五次哔哔声,省略接下来的五次,然后再次打印五次

    添加一个固定大小的队列并不能真正让它以这种方式工作。

    您以每秒 10 次的固定速率提供输入哔声, 所以添加一个 5 的输入队列只会摊销最初的 10 次哔哔声, 然后它会开始跳过所有其他的哔哔声,就像这样:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    12
    14
    16
    18
    20
    

    这(除了那些第一个元素)与原始的RateLimiter 已经做的没有什么不同:

    1
    3
    5
    7
    9
    11
    13
    15
    17
    19
    

    此外,将队列添加到RateLimiter 将要求它开始接受异步任务,这听起来不像是自然的RateLimiter 职责,因此它可能会做出糟糕的设计。

    【讨论】:

    • 我不想给它添加一个固定大小的队列。我只希望它在时间段 Y 内接受前 X 个请求。然后忽略或排队下一个传入消息(根据现有功能)。一旦间隔滴答作响,再次接受 X 并重复。希望有意义吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多