java503错误是什么_HttpClient对503错误的特殊处理

package org.apache.http.impl.client;

import java.util.HashMap;

import java.util.Map;

import org.apache.http.client.BackoffManager;

import org.apache.http.conn.routing.HttpRoute;

import org.apache.http.pool.ConnPoolControl;

import org.apache.http.util.Args;

/***

The {@code AIMDBackoffManager} applies an additive increase,* multiplicative decrease (AIMD) to managing a dynamic limit to* the number of connections allowed to a given host. You may want* to experiment with the settings for the cooldown periods and the* backoff factor to get the adaptive behavior you want.

**

Generally speaking, shorter cooldowns will lead to more steady-state* variability but faster reaction times, while longer cooldowns* will lead to more stable equilibrium behavior but slower reaction* times.

**

Similarly, higher backoff factors promote greater* utilization of available capacity at the expense of fairness* among clients. Lower backoff factors allow equal distribution of* capacity among clients (fairness) to happen faster, at the* expense of having more server capacity unused in the short term.

** @since 4.2*/

public class AIMDBackoffManager implements BackoffManager {

private final ConnPoolControl connPerRoute;

private final Clock clock;

private final Map lastRouteProbes;

private final Map lastRouteBackoffs;

private long coolDown = 5 * 1000L;

private double backoffFactor = 0.5;

private int cap = 2; // Per RFC 2616 sec 8.1.4

/*** Creates an {@code AIMDBackoffManager} to manage* per-host connection pool sizes represented by the* given {@link ConnPoolControl}.* @param connPerRoute per-host routing maximums to* be managed*/

public AIMDBackoffManager(final ConnPoolControl connPerRoute) {

this(connPerRoute, new SystemClock());

}

AIMDBackoffManager(final ConnPoolControl connPerRoute, final Clock clock) {

this.clock = clock;

this.connPerRoute = connPerRoute;

this.lastRouteProbes = new HashMap();

this.lastRouteBackoffs = new HashMap();

}

@Override

public void backOff(final HttpRoute route) {

synchronized(connPerRoute) {

final int curr = connPerRoute.getMaxPerRoute(route);

final Long lastUpdate = getLastUpdate(lastRouteBackoffs, route);

final long now = clock.getCurrentTime();

if (now - lastUpdate.longValue() < coolDown) {

return;

}

connPerRoute.setMaxPerRoute(route, getBackedOffPoolSize(curr));

lastRouteBackoffs.put(route, Long.valueOf(now));

}

}

private int getBackedOffPoolSize(final int curr) {

if (curr <= 1) {

return 1;

}

return (int)(Math.floor(backoffFactor * curr));

}

@Override

public void probe(final HttpRoute route) {

synchronized(connPerRoute) {

final int curr = connPerRoute.getMaxPerRoute(route);

final int max = (curr >= cap) ? cap : curr + 1;

final Long lastProbe = getLastUpdate(lastRouteProbes, route);

final Long lastBackoff = getLastUpdate(lastRouteBackoffs, route);

final long now = clock.getCurrentTime();

if (now - lastProbe.longValue() < coolDown || now - lastBackoff.longValue() < coolDown) {

return;

}

connPerRoute.setMaxPerRoute(route, max);

lastRouteProbes.put(route, Long.valueOf(now));

}

}

private Long getLastUpdate(final Map updates, final HttpRoute route) {

Long lastUpdate = updates.get(route);

if (lastUpdate == null) {

lastUpdate = Long.valueOf(0L);

}

return lastUpdate;

}

/*** Sets the factor to use when backing off; the new* per-host limit will be roughly the current max times* this factor. {@code Math.floor} is applied in the* case of non-integer outcomes to ensure we actually* decrease the pool size. Pool sizes are never decreased* below 1, however. Defaults to 0.5.* @param d must be between 0.0 and 1.0, exclusive.*/

public void setBackoffFactor(final double d) {

Args.check(d > 0.0 && d < 1.0, "Backoff factor must be 0.0 < f < 1.0");

backoffFactor = d;

}

/*** Sets the amount of time, in milliseconds, to wait between* adjustments in pool sizes for a given host, to allow* enough time for the adjustments to take effect. Defaults* to 5000L (5 seconds).* @param l must be positive*/

public void setCooldownMillis(final long l) {

Args.positive(coolDown, "Cool down");

coolDown = l;

}

/*** Sets the absolute maximum per-host connection pool size to* probe up to; defaults to 2 (the default per-host max).* @param cap must be >= 1*/

public void setPerHostConnectionCap(final int cap) {

Args.positive(cap, "Per host connection cap");

this.cap = cap;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值