public HttpDestination(HttpClient client, String scheme, String host, int port) { this.client = client; this.scheme = scheme; this.host = host; this.address = new Address(host, port); int maxRequestsQueued = client.getMaxRequestsQueuedPerDestination(); int capacity = Math.min(32, maxRequestsQueued); this.exchanges = new BlockingArrayQueue<>(capacity, capacity, maxRequestsQueued); int maxConnections = client.getMaxConnectionsPerDestination(); capacity = Math.min(8, maxConnections); this.idleConnections = new BlockingArrayQueue<>(capacity, capacity, maxConnections); this.activeConnections = new BlockingArrayQueue<>(capacity, capacity, maxConnections); this.requestNotifier = new RequestNotifier(client); this.responseNotifier = new ResponseNotifier(client); ProxyConfiguration proxyConfig = client.getProxyConfiguration(); proxyAddress = proxyConfig != null && proxyConfig.matches(host, port) ? new Address(proxyConfig.getHost(), proxyConfig.getPort()) : null; hostField = new HttpField(HttpHeader.HOST, host + ":" + port); }
public void send(Request request, List<Response.ResponseListener> listeners) { if (!scheme.equals(request.getScheme())) throw new IllegalArgumentException( "Invalid request scheme " + request.getScheme() + " for destination " + this); if (!getHost().equals(request.getHost())) throw new IllegalArgumentException( "Invalid request host " + request.getHost() + " for destination " + this); int port = request.getPort(); if (port >= 0 && getPort() != port) throw new IllegalArgumentException( "Invalid request port " + port + " for destination " + this); HttpConversation conversation = client.getConversation(request.getConversationID(), true); HttpExchange exchange = new HttpExchange(conversation, this, request, listeners); if (client.isRunning()) { if (exchanges.offer(exchange)) { if (!client.isRunning() && exchanges.remove(exchange)) { throw new RejectedExecutionException(client + " is stopping"); } else { LOG.debug("Queued {}", request); requestNotifier.notifyQueued(request); Connection connection = acquire(); if (connection != null) process(connection, false); } } else { throw new RejectedExecutionException( "Max requests per destination " + client.getMaxRequestsQueuedPerDestination() + " exceeded"); } } else { throw new RejectedExecutionException(client + " is stopped"); } }
protected void send(HttpRequest request, List<Response.ResponseListener> listeners) { if (!getScheme().equals(request.getScheme())) throw new IllegalArgumentException( "Invalid request scheme " + request.getScheme() + " for destination " + this); if (!getHost().equals(request.getHost())) throw new IllegalArgumentException( "Invalid request host " + request.getHost() + " for destination " + this); int port = request.getPort(); if (port >= 0 && getPort() != port) throw new IllegalArgumentException( "Invalid request port " + port + " for destination " + this); HttpExchange exchange = new HttpExchange(this, request, listeners); if (client.isRunning()) { if (enqueue(exchanges, exchange)) { if (!client.isRunning() && exchanges.remove(exchange)) { request.abort(new RejectedExecutionException(client + " is stopping")); } else { if (LOG.isDebugEnabled()) LOG.debug("Queued {} for {}", request, this); requestNotifier.notifyQueued(request); send(); } } else { if (LOG.isDebugEnabled()) LOG.debug( "Max queue size {} exceeded by {} for {}", client.getMaxRequestsQueuedPerDestination(), request, this); request.abort( new RejectedExecutionException( "Max requests per destination " + client.getMaxRequestsQueuedPerDestination() + " exceeded for " + this)); } } else { request.abort(new RejectedExecutionException(client + " is stopped")); } }
protected Queue<HttpExchange> newExchangeQueue(HttpClient client) { return new BlockingArrayQueue<>(client.getMaxRequestsQueuedPerDestination()); }