Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add callee ip in RpcContext #65

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand All @@ -14,12 +14,10 @@
import com.tencent.trpc.core.filter.spi.Filter;
import com.tencent.trpc.core.logger.Logger;
import com.tencent.trpc.core.logger.LoggerFactory;
import com.tencent.trpc.core.rpc.ConsumerInvoker;
import com.tencent.trpc.core.rpc.Invoker;
import com.tencent.trpc.core.rpc.Request;
import com.tencent.trpc.core.rpc.RequestMeta;
import com.tencent.trpc.core.rpc.Response;
import com.tencent.trpc.core.rpc.RpcContext;
import com.tencent.trpc.core.rpc.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能import *

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理

import com.tencent.trpc.core.utils.RpcContextUtils;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;

Expand All @@ -43,6 +41,7 @@ public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
ConsumerInvoker consumerInvoker = (ConsumerInvoker) invoker;
// combine with DefClusterInvocationHandler#genRequest to complete the information of the request.
prepareRequestInfoBeforeInvoke(request, consumerInvoker);
contextWithRemoteCalleeIp(context, request);
startLog(context, request);
CompletableFuture<Response> future = invoker.invoke(request).toCompletableFuture();
if (logger.isDebugEnabled()) {
Expand All @@ -60,6 +59,18 @@ public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
});
}

/**
* Set the request remote callee IP to RpcContext, with the key as CTX_CALLEE_REMOTE_IP.
*
* @param context RpcContext
* @param request Request
*/
private void contextWithRemoteCalleeIp(RpcContext context, Request request) {
Optional.ofNullable(request.getMeta().getRemoteAddress()).ifPresent(remoteAddr
-> RpcContextUtils.putValueMapValue(context, RpcContextValueKeys.CTX_CALLEE_REMOTE_IP,
remoteAddr.getAddress().getHostAddress()));
}

private void prepareRequestInfoBeforeInvoke(Request request,
ConsumerInvoker<?> consumerInvoker) {
RequestMeta meta = request.getMeta();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public int getOrder() {
public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
RpcServerContext serverContext = (RpcServerContext) (request.getContext());
prepareRequestInfoBeforeInvoke(request, (ProviderInvoker) invoker);
contextWithRemoteIp(serverContext, request);
contextWithRemoteCallerIp(serverContext, request);
startLog(serverContext, request);
CompletableFuture<Response> future = invoker.invoke(request).toCompletableFuture();
if (logger.isDebugEnabled()) {
Expand All @@ -62,12 +62,12 @@ public CompletionStage<Response> filter(Invoker<?> invoker, Request request) {
}

/**
* Set the request remote IP to RpcServerContext, with the key as CTX_CALLER_REMOTE_IP.
* Set the request remote caller IP to RpcServerContext, with the key as CTX_CALLER_REMOTE_IP.
*
* @param serverContext RpcServerContext
* @param request Request
*/
private void contextWithRemoteIp(RpcServerContext serverContext, Request request) {
private void contextWithRemoteCallerIp(RpcServerContext serverContext, Request request) {
Optional.ofNullable(request.getMeta().getRemoteAddress()).ifPresent(remoteAddr
-> RpcContextUtils.putValueMapValue(serverContext, RpcContextValueKeys.CTX_CALLER_REMOTE_IP,
remoteAddr.getAddress().getHostAddress()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ public class RpcContextValueKeys {
*/
public static final String CTX_TELEMETRY_TRACE_SPAN = "ctx_telemetry_trace_span";
/**
* Remote IP.
* Caller Remote IP.
*/
public static final String CTX_CALLER_REMOTE_IP = "ctx_caller_remote_ip";
/**
* Callee Remote IP.
*/
public static final String CTX_CALLEE_REMOTE_IP = "ctx_callee_remote_ip";
/**
* Full link timeout key.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import com.tencent.trpc.core.exception.ErrorCode;
import com.tencent.trpc.core.exception.TRpcException;
import com.tencent.trpc.core.exception.TransportException;
import com.tencent.trpc.core.rpc.RpcClient;
import com.tencent.trpc.core.rpc.RpcClientContext;
import com.tencent.trpc.core.rpc.RpcServer;
import com.tencent.trpc.core.rpc.RpcServerManager;
import com.tencent.trpc.core.rpc.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不能import*,import具体的类

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已处理

import com.tencent.trpc.core.transport.Channel;
import com.tencent.trpc.core.transport.handler.ChannelHandlerAdapter;
import com.tencent.trpc.core.utils.Charsets;
Expand Down Expand Up @@ -241,6 +238,7 @@ public void serverNormalTest() {
.println(">>>>>>>>>" + new String((byte[]) (context.getRspAttachMap().get("key"))));
assertEquals(new String((byte[]) (context.getRspAttachMap().get("key")), Charsets.UTF_8),
"abc");
assertEquals("127.0.0.1",context.getValueMap().get(RpcContextValueKeys.CTX_CALLEE_REMOTE_IP));
}

@Test
Expand Down
Loading