01: package net.javacoding.jspider.core.throttle.impl;
02:
03: import net.javacoding.jspider.core.throttle.Throttle;
04:
05: /**
06: * Throttle implementation that forces at least x milliseconds between two
07: * subsequent requests on a certain host.
08: *
09: * $Id: DistributedLoadThrottleImpl.java,v 1.2 2003/02/27 16:47:50 vanrogu Exp $
10: *
11: * @author Günther Van Roey
12: */
13: public class DistributedLoadThrottleImpl implements Throttle {
14:
15: /** min. milliseconds between two subsequent calls. */
16: protected int milliseconds;
17:
18: /** last allowed time for a fetch. */
19: protected long lastAllow;
20:
21: /**
22: * Constructor taking the amount of milliseconds to wait between
23: * two subsequent requests as a parameter.
24: * @param milliseconds minimum nr of milliseconds
25: */
26: public DistributedLoadThrottleImpl(int milliseconds) {
27: this .milliseconds = milliseconds;
28: lastAllow = System.currentTimeMillis() - milliseconds;
29: }
30:
31: /**
32: * This method will block spider threads until they're allowed
33: * to do a request.
34: */
35: public synchronized void throttle() {
36:
37: long this Time = System.currentTimeMillis();
38: long scheduledTime = lastAllow + milliseconds;
39:
40: if (scheduledTime > this Time) {
41: try {
42: Thread.sleep(scheduledTime - this Time);
43: } catch (InterruptedException e) {
44: Thread.currentThread().interrupt();
45: }
46: }
47:
48: lastAllow = System.currentTimeMillis();
49: }
50: }
|