Skip to content

Commit

Permalink
extend ErrorReason for camellia-http-accelerate-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
caojiajun committed Jul 21, 2023
1 parent 8b201ac commit fa37e6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public enum ErrorReason {
UPSTREAM_SERVER_SELECT_FAIL(4),
UPSTREAM_CONNECT_FAIL(5),
UPSTREAM_ERROR(6),
UPSTREAM_NOT_2XX_CODE(7),
UPSTREAM_NOT_SUPPORT_METHOD(8),
;

private final int value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.netease.nim.camellia.http.accelerate.proxy.core.upstream;

import com.netease.nim.camellia.http.accelerate.proxy.core.context.ErrorReason;
import com.netease.nim.camellia.http.accelerate.proxy.core.context.LogBean;
import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyRequest;
import com.netease.nim.camellia.http.accelerate.proxy.core.context.ProxyResponse;
import com.netease.nim.camellia.http.accelerate.proxy.core.conf.DynamicConf;
Expand Down Expand Up @@ -158,7 +159,12 @@ public CompletableFuture<ProxyResponse> send(ProxyRequest proxyRequest) {
RequestBody body = RequestBody.create(requestBody, MediaType.get(contentType));
builder.patch(body);
} else {
proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_NOT_SUPPORT_METHOD);
future.complete(new ProxyResponse(Constants.BAD_GATEWAY, proxyRequest.getLogBean()));
if (logger.isWarnEnabled()) {
LogBean logBean = proxyRequest.getLogBean();
logger.warn("not support method, host = {}, path = {}, method = {}", logBean.getHost(), logBean.getPath(), method.name());
}
return future;
}
proxyRequest.getLogBean().setUpstreamSendTime(System.currentTimeMillis());
Expand Down Expand Up @@ -187,7 +193,11 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO
httpHeaders.set(name, value);
}
ByteBuf byteBuf = Unpooled.wrappedBuffer(response.body().bytes());
DefaultFullHttpResponse rep = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.valueOf(response.code()), byteBuf, httpHeaders, new DefaultHttpHeaders());
int code = response.code();
if (!(code >= 200 && code <= 299)) {
proxyRequest.getLogBean().setErrorReason(ErrorReason.UPSTREAM_NOT_2XX_CODE);
}
DefaultFullHttpResponse rep = new DefaultFullHttpResponse(request.protocolVersion(), HttpResponseStatus.valueOf(code), byteBuf, httpHeaders, new DefaultHttpHeaders());
future.complete(new ProxyResponse(rep, proxyRequest.getLogBean()));
}
});
Expand Down

0 comments on commit fa37e6e

Please sign in to comment.