aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/core/communication/GatewayThrottler.java
blob: 4041ec21c44296da065a7c21de1b9c3e0e729154 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core.communication;

import java.util.Random;

/**
 * When the gateways says it can not handle more load, we should send less load. That is the responsibility
 * of this component
 *
 * @author dybis
 */
public class GatewayThrottler {
    private long backOffTimeMs = 0;
    private final long maxSleepTimeMs;
    private static Random random = new Random();

    public GatewayThrottler(long maxSleepTimeMs) {
        this.maxSleepTimeMs = maxSleepTimeMs;
    }

    public void handleCall(int transientErrors) {
        if (transientErrors > 0) {
            backOffTimeMs = Math.min(maxSleepTimeMs, backOffTimeMs + distribute(100));
        } else {
            backOffTimeMs = Math.max(0, backOffTimeMs - distribute(10));
        }
        sleepMs(backOffTimeMs);
    }

    protected void sleepMs(long sleepTime) {
        try {
            if (backOffTimeMs > 0L) {
                Thread.sleep(backOffTimeMs);
            }
        } catch (InterruptedException e) {
            // Do nothing
        }
    }

    public int distribute(int expected) {
        double factor = 0.5 + random.nextDouble();
        Double result = expected * factor;
        return result.intValue();
    }
}