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

104 lines
4.3 KiB
Java
Raw Normal View History

2021-02-13 03:56:06 +00:00
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2021-01-22 07:40:02 +00:00
package io.github.ehlxr.did.server.http;
2018-08-14 07:21:56 +00:00
2021-02-09 09:36:11 +00:00
import io.github.ehlxr.did.SdkProto;
2021-01-22 07:40:02 +00:00
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.common.NettyUtil;
2021-02-07 10:21:08 +00:00
import io.github.ehlxr.did.common.Result;
2021-03-04 03:24:45 +00:00
import io.github.ehlxr.did.generator.SnowFlake;
2018-08-14 07:21:56 +00:00
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());
/**
*
*/
2021-01-18 08:17:14 +00:00
private final Semaphore semaphore = new Semaphore(Constants.HANDLE_HTTP_TPS);
private final SnowFlake snowFlake;
2018-08-14 07:21:56 +00:00
public HttpServerHandler(SnowFlake snowFlake) {
this.snowFlake = snowFlake;
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
2021-02-08 09:15:36 +00:00
logger.debug("http server handler receive request {}", request);
2018-08-14 07:21:56 +00:00
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
2021-02-07 10:21:08 +00:00
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;
}
2018-08-14 10:37:34 +00:00
if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) {
2021-02-08 09:15:36 +00:00
long id = snowFlake.nextId();
2021-02-07 10:21:08 +00:00
2021-02-08 09:15:36 +00:00
status = HttpResponseStatus.OK;
result = Result.success(SdkProto.newBuilder().did(id).build());
2018-08-14 07:21:56 +00:00
} else {
2018-08-14 10:37:34 +00:00
String info = String.format("HttpServerHandler tryAcquire semaphore timeout, %dms, waiting thread " + "nums: %d availablePermit: %d",
Constants.ACQUIRE_TIMEOUTMILLIS, this.semaphore.getQueueLength(), this.semaphore.availablePermits());
2021-02-08 09:15:36 +00:00
logger.error(info);
2021-02-07 10:21:08 +00:00
status = HttpResponseStatus.SERVICE_UNAVAILABLE;
result = Result.fail(status.code(), info);
2018-08-14 07:21:56 +00:00
}
2021-02-07 10:21:08 +00:00
response.setStatus(status)
.content().writeBytes(result.toString().getBytes());
2021-02-09 14:45:13 +00:00
logger.debug("http server handler write response {} result {} to channel", status, result);
2018-08-14 07:21:56 +00:00
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Channel channel = ctx.channel();
2021-02-09 14:45:13 +00:00
logger.error("channel {} will be closed, 'cause of ", NettyUtil.parseRemoteAddr(channel), cause);
2018-08-14 07:21:56 +00:00
NettyUtil.closeChannel(channel);
}
}