Optimized code
This commit is contained in:
@@ -37,7 +37,6 @@ public class HttpServer extends BaseServer {
|
||||
return new HttpServerBuilder();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void shutdown() {
|
||||
logger.info("HttpServer shutdowning......");
|
||||
@@ -76,10 +75,6 @@ public class HttpServer extends BaseServer {
|
||||
private HttpServerBuilder() {
|
||||
}
|
||||
|
||||
public static HttpServerBuilder aHttpServer() {
|
||||
return new HttpServerBuilder();
|
||||
}
|
||||
|
||||
public HttpServerBuilder snowFlake(SnowFlake snowFlake) {
|
||||
this.snowFlake = snowFlake;
|
||||
return this;
|
||||
|
||||
@@ -2,6 +2,8 @@ package io.github.ehlxr.did.server.http;
|
||||
|
||||
import io.github.ehlxr.did.common.Constants;
|
||||
import io.github.ehlxr.did.common.NettyUtil;
|
||||
import io.github.ehlxr.did.common.Result;
|
||||
import io.github.ehlxr.did.common.SdkProto;
|
||||
import io.github.ehlxr.did.core.SnowFlake;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
@@ -32,22 +34,45 @@ public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpReque
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
|
||||
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
|
||||
response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
|
||||
Result<?> result;
|
||||
HttpResponseStatus status;
|
||||
|
||||
if (!"/did".equals(request.uri())) {
|
||||
result = Result.fail(HttpResponseStatus.NOT_FOUND.code(), HttpResponseStatus.NOT_FOUND.reasonPhrase());
|
||||
response.setStatus(HttpResponseStatus.NOT_FOUND)
|
||||
.content().writeBytes(result.toString().getBytes());
|
||||
|
||||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
long id = snowFlake.nextId();
|
||||
logger.info("HttpServerHandler id is: {}", id);
|
||||
response.content().writeBytes(("" + id).getBytes());
|
||||
|
||||
status = HttpResponseStatus.OK;
|
||||
result = Result.success(SdkProto.newBuilder().did(id).build());
|
||||
} catch (Exception e) {
|
||||
semaphore.release();
|
||||
logger.error("HttpServerHandler error", e);
|
||||
|
||||
status = HttpResponseStatus.INTERNAL_SERVER_ERROR;
|
||||
result = Result.fail(status.code(), status.reasonPhrase());
|
||||
}
|
||||
} else {
|
||||
String info = String.format("HttpServerHandler tryAcquire semaphore timeout, %dms, waiting thread " + "nums: %d availablePermit: %d",
|
||||
Constants.ACQUIRE_TIMEOUTMILLIS, this.semaphore.getQueueLength(), this.semaphore.availablePermits());
|
||||
logger.warn(info);
|
||||
throw new Exception(info);
|
||||
|
||||
status = HttpResponseStatus.SERVICE_UNAVAILABLE;
|
||||
result = Result.fail(status.code(), info);
|
||||
}
|
||||
|
||||
response.setStatus(status)
|
||||
.content().writeBytes(result.toString().getBytes());
|
||||
|
||||
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,22 +34,18 @@ public class SdkServerHandler extends SimpleChannelInboundHandler<SdkProto> {
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, SdkProto sdkProto) throws Exception {
|
||||
if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
sdkProto.setDid(snowFlake.nextId());
|
||||
sdkProto.setDid(snowFlake.nextId());
|
||||
|
||||
ctx.channel().writeAndFlush(sdkProto).addListener((ChannelFutureListener) channelFuture -> semaphore.release());
|
||||
} catch (Exception e) {
|
||||
semaphore.release();
|
||||
logger.error("SdkServerhandler error", e);
|
||||
}
|
||||
semaphore.release();
|
||||
} else {
|
||||
sdkProto.setDid(-1);
|
||||
ctx.channel().writeAndFlush(sdkProto);
|
||||
String info = String.format("SdkServerHandler tryAcquire semaphore timeout, %dms, waiting thread " + "nums: %d availablePermit: %d",
|
||||
Constants.ACQUIRE_TIMEOUTMILLIS, this.semaphore.getQueueLength(), this.semaphore.availablePermits());
|
||||
logger.warn(info);
|
||||
throw new Exception(info);
|
||||
logger.error(info);
|
||||
}
|
||||
|
||||
ctx.channel().
|
||||
writeAndFlush(sdkProto).
|
||||
addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user