did/did-server/src/main/java/io/github/ehlxr/did/server/http/HttpServerHandler.java

76 lines
2.8 KiB
Java

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.core.SnowFlake;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* @author ehlxr
*/
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private final Logger logger = LoggerFactory.getLogger(getClass());
/**
* 通过信号量来控制流量
*/
private final Semaphore semaphore = new Semaphore(Constants.HANDLE_HTTP_TPS);
private final SnowFlake snowFlake;
public HttpServerHandler(SnowFlake snowFlake) {
this.snowFlake = snowFlake;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
String uri = getUriNoSprit(request);
logger.info("request uri is: {}", uri);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
// if (Constants.HTTP_REQUEST.equals(uri)) {
if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) {
try {
long id = snowFlake.nextId();
logger.info("HttpServerHandler id is: {}", id);
response.content().writeBytes(("" + id).getBytes());
} catch (Exception e) {
semaphore.release();
logger.error("HttpServerHandler error", e);
}
} 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);
}
// } else {
// response.content().writeBytes(("Unsupported uri: " + uri).getBytes());
// }
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Channel channel = ctx.channel();
logger.error("HttpServerHandler channel [{}] error and will be closed", NettyUtil.parseRemoteAddr(channel), cause);
NettyUtil.closeChannel(channel);
}
private String getUriNoSprit(FullHttpRequest request) {
String uri = request.uri();
if (uri.startsWith("/")) {
uri = uri.substring(1);
}
return uri;
}
}