Skip to content

Commit

Permalink
Changing throttle configuration to accept read/write limits instead o…
Browse files Browse the repository at this point in the history
…f GlobalTrafficShapingHandler.

- Proxy server accepts read/write limits in bytes/second.
- Added getters for read/write limits.
  • Loading branch information
mreedell committed Aug 25, 2014
1 parent d22b7b1 commit c7395c3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/main/java/org/littleshoot/proxy/HttpProxyServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ public interface HttpProxyServer {
* @return
*/
InetSocketAddress getListenAddress();

/**
* <p>
* Set the read/write throttle bandwidths (in bytes/second) for this proxy.
* </p>
* @param readThrottleBytesPerSecond
* @param writeThrottleBytesPerSecond
*/
void setThrottle(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.littleshoot.proxy;

import io.netty.handler.traffic.GlobalTrafficShapingHandler;

import java.net.InetSocketAddress;

/**
Expand Down Expand Up @@ -287,12 +285,13 @@ HttpProxyServerBootstrap withConnectTimeout(

/**
* <p>
* Specify a global traffic shaping handler for this proxy server.
* Specify the read and/or write bandwidth throttles for this proxy server. 0 indicates not throttling.
* </p>
* @param globalTrafficShapingHandler
* @param readThrottleBytesPerSecond
* @param writeThrottleBytesPerSecond
* @return
*/
HttpProxyServerBootstrap withGlobalTrafficShapingHandler(GlobalTrafficShapingHandler globalTrafficShapingHandler);
HttpProxyServerBootstrap withThrottling(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond);

/**
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -183,6 +185,10 @@ public static HttpProxyServerBootstrap bootstrapFromFile(String path) {
* server
* @param serverResolver
* the {@link HostResolver} to use for resolving server addresses
* @param readThrottleBytesPerSecond
* read throttle bandwidth
* @param writeThrottleBytesPerSecond
* write throttle bandwidth
*/
private DefaultHttpProxyServer(ServerGroup serverGroup,
TransportProtocol transportProtocol,
Expand All @@ -198,7 +204,8 @@ private DefaultHttpProxyServer(ServerGroup serverGroup,
Collection<ActivityTracker> activityTrackers,
int connectTimeout,
HostResolver serverResolver,
GlobalTrafficShapingHandler globalTrafficShapingHandler) {
long readThrottleBytesPerSecond,
long writeThrottleBytesPerSecond) {
this.serverGroup = serverGroup;
this.transportProtocol = transportProtocol;
this.address = address;
Expand All @@ -215,7 +222,9 @@ private DefaultHttpProxyServer(ServerGroup serverGroup,
}
this.connectTimeout = connectTimeout;
this.serverResolver = serverResolver;
this.globalTrafficShapingHandler = globalTrafficShapingHandler;

ScheduledExecutorService executor = new ScheduledThreadPoolExecutor(50);
this.globalTrafficShapingHandler = new GlobalTrafficShapingHandler(executor, writeThrottleBytesPerSecond, readThrottleBytesPerSecond);
}

boolean isTransparent() {
Expand All @@ -242,9 +251,18 @@ public HostResolver getServerResolver() {
public InetSocketAddress getListenAddress() {
return address;
}

public GlobalTrafficShapingHandler getGlobalTrafficShapingHandler() {
return globalTrafficShapingHandler;

@Override
public void setThrottle(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond) {
globalTrafficShapingHandler.configure(writeThrottleBytesPerSecond, readThrottleBytesPerSecond);
}

public long getReadThrottle() {
return globalTrafficShapingHandler.getReadLimit();
}

public long getWriteThrottle() {
return globalTrafficShapingHandler.getWriteLimit();
}

@Override
Expand All @@ -256,7 +274,8 @@ public HttpProxyServerBootstrap clone() {
chainProxyManager,
mitmManager, filtersSource, transparent,
idleConnectionTimeout, activityTrackers, connectTimeout,
serverResolver, globalTrafficShapingHandler);
serverResolver, globalTrafficShapingHandler.getReadLimit(),
globalTrafficShapingHandler.getWriteLimit());
}

@Override
Expand Down Expand Up @@ -560,7 +579,8 @@ private static class DefaultHttpProxyServerBootstrap implements
private Collection<ActivityTracker> activityTrackers = new ConcurrentLinkedQueue<ActivityTracker>();
private int connectTimeout = 40000;
private HostResolver serverResolver = new DefaultHostResolver();
private GlobalTrafficShapingHandler globalTrafficShapingHandler;
private long readThrottleBytesPerSecond;
private long writeThrottleBytesPerSecond;

private DefaultHttpProxyServerBootstrap() {
}
Expand All @@ -578,7 +598,7 @@ private DefaultHttpProxyServerBootstrap(
boolean transparent, int idleConnectionTimeout,
Collection<ActivityTracker> activityTrackers,
int connectTimeout, HostResolver serverResolver,
GlobalTrafficShapingHandler globalTrafficShapingHandler) {
long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond) {
this.original = original;
this.transportProtocol = transportProtocol;
this.address = address;
Expand All @@ -595,7 +615,8 @@ private DefaultHttpProxyServerBootstrap(
}
this.connectTimeout = connectTimeout;
this.serverResolver = serverResolver;
this.globalTrafficShapingHandler = globalTrafficShapingHandler;
this.readThrottleBytesPerSecond = readThrottleBytesPerSecond;
this.writeThrottleBytesPerSecond = writeThrottleBytesPerSecond;
}

private DefaultHttpProxyServerBootstrap(Properties props) {
Expand Down Expand Up @@ -745,8 +766,9 @@ public HttpProxyServerBootstrap plusActivityTracker(
}

@Override
public HttpProxyServerBootstrap withGlobalTrafficShapingHandler(GlobalTrafficShapingHandler globalTrafficShapingHandler) {
this.globalTrafficShapingHandler = globalTrafficShapingHandler;
public HttpProxyServerBootstrap withThrottling(long readThrottleBytesPerSecond, long writeThrottleBytesPerSecond) {
this.readThrottleBytesPerSecond = readThrottleBytesPerSecond;
this.writeThrottleBytesPerSecond = writeThrottleBytesPerSecond;
return this;
}

Expand All @@ -771,7 +793,7 @@ transportProtocol, determineListenAddress(),
proxyAuthenticator, chainProxyManager, mitmManager,
filtersSource, transparent,
idleConnectionTimeout, activityTrackers, connectTimeout,
serverResolver, globalTrafficShapingHandler);
serverResolver, readThrottleBytesPerSecond, writeThrottleBytesPerSecond);
}

private InetSocketAddress determineListenAddress() {
Expand Down

0 comments on commit c7395c3

Please sign in to comment.