forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Develop refactor request context (alibaba#12331)
* Add RequestContext and RequestContextHolder. * build RequestContext when request start. * Refactor ClientAttributesFilter with RequestContext. * InstanceController get client ip from request context. * SubscribeServiceRequestHandler get app from requestcontext. * config http api support use request context get user, app and source ip. * Rename nacos request context filter. * Unified naming request get source ip by request context. * For checkstyle.
- Loading branch information
1 parent
2aa9fc5
commit 2233e65
Showing
41 changed files
with
1,952 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
core/src/main/java/com/alibaba/nacos/core/context/RequestContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.core.context; | ||
|
||
import com.alibaba.nacos.core.context.addition.AuthContext; | ||
import com.alibaba.nacos.core.context.addition.BasicContext; | ||
import com.alibaba.nacos.core.context.addition.EngineContext; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Nacos request context. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class RequestContext { | ||
|
||
/** | ||
* Optional, the request id. | ||
* <ul> | ||
* <li>For HTTP request, the id not usage, will generate automatically.</li> | ||
* <li>For GRPC, the id is same with real request id.</li> | ||
* </ul> | ||
*/ | ||
private String requestId; | ||
|
||
/** | ||
* Request start timestamp. | ||
*/ | ||
private final long requestTimestamp; | ||
|
||
private final BasicContext basicContext; | ||
|
||
private final EngineContext engineContext; | ||
|
||
private final AuthContext authContext; | ||
|
||
private final Map<String, Object> extensionContexts; | ||
|
||
RequestContext(long requestTimestamp) { | ||
this.requestId = UUID.randomUUID().toString(); | ||
this.requestTimestamp = requestTimestamp; | ||
this.basicContext = new BasicContext(); | ||
this.engineContext = new EngineContext(); | ||
this.authContext = new AuthContext(); | ||
this.extensionContexts = new HashMap<>(1); | ||
} | ||
|
||
public void setRequestId(String requestId) { | ||
this.requestId = requestId; | ||
} | ||
|
||
public String getRequestId() { | ||
return requestId; | ||
} | ||
|
||
public long getRequestTimestamp() { | ||
return requestTimestamp; | ||
} | ||
|
||
public BasicContext getBasicContext() { | ||
return basicContext; | ||
} | ||
|
||
public EngineContext getEngineContext() { | ||
return engineContext; | ||
} | ||
|
||
public AuthContext getAuthContext() { | ||
return authContext; | ||
} | ||
|
||
public Object getExtensionContext(String key) { | ||
return extensionContexts.get(key); | ||
} | ||
|
||
public void addExtensionContext(String key, Object value) { | ||
extensionContexts.put(key, value); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
core/src/main/java/com/alibaba/nacos/core/context/RequestContextHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 1999-2023 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.alibaba.nacos.core.context; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Holder for request context for each worker thread. | ||
* | ||
* @author xiweng.yy | ||
*/ | ||
public class RequestContextHolder { | ||
|
||
private static final Supplier<RequestContext> REQUEST_CONTEXT_FACTORY = () -> { | ||
long requestTimestamp = System.currentTimeMillis(); | ||
return new RequestContext(requestTimestamp); | ||
}; | ||
|
||
private static final ThreadLocal<RequestContext> CONTEXT_HOLDER = ThreadLocal.withInitial(REQUEST_CONTEXT_FACTORY); | ||
|
||
public static RequestContext getContext() { | ||
return CONTEXT_HOLDER.get(); | ||
} | ||
|
||
public static void removeContext() { | ||
CONTEXT_HOLDER.remove(); | ||
} | ||
} |
Oops, something went wrong.