-
Notifications
You must be signed in to change notification settings - Fork 922
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a way to customize context propagation using its own storage
Motivation: Modifications: - Add `ContextStorage` and its default implementation `ContextStorageThreadLocal`. - Add `ContextStorageProvier` so that a user customizes the `ContextStorage` using Java SPI Result:
- Loading branch information
Showing
15 changed files
with
371 additions
and
103 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
45 changes: 45 additions & 0 deletions
45
core/src/main/java/com/linecorp/armeria/common/ContextStorage.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,45 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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: | ||
* | ||
* https://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.linecorp.armeria.common; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.linecorp.armeria.common.util.UnstableApi; | ||
|
||
/** | ||
* Storage. | ||
*/ | ||
@UnstableApi | ||
public interface ContextStorage { | ||
|
||
/** | ||
* Push. | ||
*/ | ||
@Nullable | ||
<T extends RequestContext> T push(RequestContext toPush); | ||
|
||
/** | ||
* Pop. | ||
*/ | ||
void pop(@Nullable RequestContext toRestore); | ||
|
||
/** | ||
* Current. | ||
*/ | ||
@Nullable | ||
<T extends RequestContext> T currentOrNull(); | ||
} |
32 changes: 32 additions & 0 deletions
32
core/src/main/java/com/linecorp/armeria/common/ContextStorageProvider.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,32 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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: | ||
* | ||
* https://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.linecorp.armeria.common; | ||
|
||
import com.linecorp.armeria.common.util.UnstableApi; | ||
|
||
/** | ||
* Creates a new {@link ContextStorage} dynamically via Java SPI (Service Provider Interface). | ||
*/ | ||
@UnstableApi | ||
@FunctionalInterface | ||
public interface ContextStorageProvider { | ||
|
||
/** | ||
* Creates a new {@link ContextStorage}. | ||
*/ | ||
ContextStorage newContextStorage(); | ||
} |
59 changes: 59 additions & 0 deletions
59
core/src/main/java/com/linecorp/armeria/common/ContextStorageThreadLocal.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,59 @@ | ||
/* | ||
* Copyright 2020 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you 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: | ||
* | ||
* https://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.linecorp.armeria.common; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import com.linecorp.armeria.common.util.UnstableApi; | ||
|
||
import io.netty.util.concurrent.FastThreadLocal; | ||
import io.netty.util.internal.InternalThreadLocalMap; | ||
|
||
/** | ||
* A {@link ContextStorage} that uses thread-local to store {@link RequestContext}. | ||
* Override this. | ||
*/ | ||
@UnstableApi | ||
public class ContextStorageThreadLocal implements ContextStorage { | ||
|
||
private static final FastThreadLocal<RequestContext> context = new FastThreadLocal<>(); | ||
|
||
@Nullable | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends RequestContext> T push(RequestContext toPush) { | ||
requireNonNull(toPush, "toPush"); | ||
final InternalThreadLocalMap map = InternalThreadLocalMap.get(); | ||
final RequestContext oldCtx = context.get(map); | ||
context.set(map, toPush); | ||
return (T) oldCtx; | ||
} | ||
|
||
@Override | ||
public void pop(@Nullable RequestContext toRestore) { | ||
context.set(toRestore); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T extends RequestContext> T currentOrNull() { | ||
return (T) context.get(); | ||
} | ||
} |
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
82 changes: 0 additions & 82 deletions
82
core/src/main/java/com/linecorp/armeria/internal/common/RequestContextThreadLocal.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.