summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/btr/proxy/selector/misc/ProxyListFallbackSelector.java
blob: e5019527d123861a805bc3f0836e5f61355e67a4 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.btr.proxy.selector.misc;

import java.io.IOException;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.btr.proxy.util.ProxyUtil;

/*****************************************************************************
 * Implements a fallback selector to warp it around an existing ProxySelector.
 * This will remove proxies from a list of proxies and implement an automatic
 * retry mechanism.
 *  
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2011
 ****************************************************************************/

public class ProxyListFallbackSelector extends ProxySelector {

	// Retry a unresponsive proxy after 10 minutes per default.
	private static final int DEFAULT_RETRY_DELAY = 1000*60*10;
	
	private ProxySelector delegate;
	private ConcurrentHashMap<SocketAddress, Long> failedDelayCache;
	private long retryAfterMs; 
	
	/*************************************************************************
	 * Constructor
	 * @param delegate the delegate to use.
	 ************************************************************************/
	
	public ProxyListFallbackSelector(ProxySelector delegate) {
		this(DEFAULT_RETRY_DELAY, delegate);
	}
	
	/*************************************************************************
	 * Constructor
	 * @param retryAfterMs the "retry delay" as amount of milliseconds. 
	 * @param delegate the delegate to use.
	 ************************************************************************/
	
	public ProxyListFallbackSelector(long retryAfterMs, ProxySelector delegate) {
		super();
		this.failedDelayCache = new ConcurrentHashMap<SocketAddress, Long>();
		this.delegate = delegate;
		this.retryAfterMs = retryAfterMs;
	}
	
	/*************************************************************************
	 * connectFailed
	 * @see java.net.ProxySelector#connectFailed(java.net.URI, java.net.SocketAddress, java.io.IOException)
	 ************************************************************************/

	@Override
	public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
		this.failedDelayCache.put(sa, System.currentTimeMillis());
		this.delegate.connectFailed(uri, sa, ioe);
	}

	/*************************************************************************
	 * select
	 * @see java.net.ProxySelector#select(java.net.URI)
	 ************************************************************************/

	@Override
	public List<Proxy> select(URI uri) {
		cleanupCache();
		List<Proxy> proxyList = this.delegate.select(uri);
		List<Proxy> result = filterUnresponsiveProxiesFromList(proxyList);
		if (result.isEmpty()) {
			return ProxyUtil.noProxyList();
		}
		return result;
	}

	/*************************************************************************
	 * Cleanup the entries from the cache that are no longer unresponsive.
	 ************************************************************************/
	
	private void cleanupCache() {
		Iterator<Entry<SocketAddress, Long>> it 
					= this.failedDelayCache.entrySet().iterator();
		while (it.hasNext()) {
			Entry<SocketAddress, Long> e = it.next();
			Long lastFailTime = e.getValue();
			if (retryDelayHasPassedBy(lastFailTime)) {
				it.remove();
			}
		}
	}

	/*************************************************************************
	 * @param proxyList
	 * @return
	 ************************************************************************/
	
	private List<Proxy> filterUnresponsiveProxiesFromList(List<Proxy> proxyList) {
		if (this.failedDelayCache.isEmpty()) {
			return proxyList;
		}
		List<Proxy> result = new ArrayList<Proxy>(proxyList.size());
		for (Proxy proxy : proxyList) {
			if (isDirect(proxy) || isNotUnresponsive(proxy)) {
				result.add(proxy);
			}
		}
		return result;
	}

	/*************************************************************************
	 * @param proxy
	 * @return
	 ************************************************************************/
	
	private boolean isDirect(Proxy proxy) {
		return Proxy.NO_PROXY.equals(proxy);
	}

	/*************************************************************************
	 * @param proxy
	 * @return
	 ************************************************************************/
	
	private boolean isNotUnresponsive(Proxy proxy) {
		Long lastFailTime = this.failedDelayCache.get(proxy.address());
		return retryDelayHasPassedBy(lastFailTime);
	}

	/*************************************************************************
	 * @param lastFailTime
	 * @return
	 ************************************************************************/
	
	private boolean retryDelayHasPassedBy(Long lastFailTime) {
		return lastFailTime == null 
				|| lastFailTime + this.retryAfterMs < System.currentTimeMillis();
	}

	/*************************************************************************
	 * Only used for unit testing not part of the public API.
	 * @param retryAfterMs The retryAfterMs to set.
	 ************************************************************************/
	
	final void setRetryAfterMs(long retryAfterMs) {
		this.retryAfterMs = retryAfterMs;
	}
	
	

}