Skip to content

Commit

Permalink
add http proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ohun committed Feb 17, 2016
1 parent 0855e4a commit e7749a7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions conf-daily.properties
Expand Up @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi
force_write_redis_group_info = true
connection_server_port = 20882
gateway_server_port = 4000
dns_mapping=111.1.57.148=127.0.0.1
1 change: 1 addition & 0 deletions conf-dev.properties
Expand Up @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi
force_write_redis_group_info = true
connection_server_port = 20882
gateway_server_port = 4000
dns_mapping=127.0.0.1=127.0.0.1
1 change: 1 addition & 0 deletions conf-online.properties
Expand Up @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi
force_write_redis_group_info = false
connection_server_port = 3000
gateway_server_port = 4000
dns_mapping=127.0.0.1=127.0.0.1
1 change: 1 addition & 0 deletions conf-pre.properties
Expand Up @@ -10,3 +10,4 @@ public_key = MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCghPCWCobG8nTD24juwSVataW7iVi
force_write_redis_group_info = false
connection_server_port = 3000
gateway_server_port = 4000
dns_mapping=127.0.0.1=127.0.0.1
1 change: 1 addition & 0 deletions mpush-cs/src/main/resources/config.properties
Expand Up @@ -33,3 +33,4 @@ zk_digest = ${zk_digest}
redis_group = ${redis_group}
force_write_redis_group_info = ${force_write_redis_group_info}
jvm_log_path = /opt/shinemo/mpush/
dns_mapping=${dns_mapping}
Expand Up @@ -84,8 +84,9 @@ public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
writeRequest(future.channel(), info);
} else {
info.cancel();
info.tryDone();
info.callback.onFailure(504, "Gateway Timeout");
LOGGER.debug("request failure request=%s", info);
}
}
});
Expand Down Expand Up @@ -128,8 +129,9 @@ private void writeRequest(Channel channel, RequestInfo info) {
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
RequestInfo requestInfo = future.channel().attr(key).get();
requestInfo.cancel();
requestInfo.tryDone();
requestInfo.callback.onFailure(503, "Service Unavailable");
LOGGER.debug("request failure request=%s", requestInfo);
tryRelease(future.channel());
}
}
Expand All @@ -142,9 +144,9 @@ private class HttpClientHandler extends ChannelHandlerAdapter {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
RequestInfo info = ctx.channel().attr(key).get();
LOGGER.error("http client caught an error, info=" + info, cause);
LOGGER.error("http client caught an error, info=%s", info, cause);
try {
if (info.cancel()) {
if (info.tryDone()) {
info.callback.onException(cause);
}
} finally {
Expand All @@ -157,7 +159,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
RequestInfo info = ctx.channel().attr(key).get();
if (info == null) return;
try {
if (info.cancel()) {
if (info.tryDone()) {
HttpCallback callback = info.callback;
HttpRequest request = info.request;
HttpResponse response = (HttpResponse) msg;
Expand All @@ -173,6 +175,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}
}
callback.onResponse(response);
LOGGER.debug("request done request=%s", info);
}
} finally {
tryRelease(ctx.channel());
Expand Down
Expand Up @@ -11,7 +11,9 @@ public class RequestInfo implements TimerTask {
HttpCallback callback;
HttpRequest request;
String host;
int timeout = 5000;//5s
int timeout = 10000;//5s
long startTime = System.currentTimeMillis();
long endTime = System.currentTimeMillis();

public RequestInfo(HttpRequest request, HttpCallback callback) {
this.callback = callback;
Expand All @@ -37,13 +39,13 @@ public RequestInfo setTimeout(int timeout) {

@Override
public void run(Timeout timeout) throws Exception {
if (!cancelled.get()) {
cancelled.set(true);
if (tryDone()) {
callback.onTimeout();
}
}

public boolean cancel() {
public boolean tryDone() {
endTime = System.currentTimeMillis();
return cancelled.compareAndSet(false, true);
}

Expand All @@ -54,6 +56,7 @@ public String toString() {
", request=" + request +
", host='" + host + '\'' +
", timeout=" + timeout +
", costTime=" + (startTime - endTime) +
'}';
}
}

0 comments on commit e7749a7

Please sign in to comment.