Skip to content

Commit

Permalink
feat(request context): clear context after request is completed
Browse files Browse the repository at this point in the history
  • Loading branch information
kenluluuuluuuuu committed Apr 10, 2024
1 parent 01036ea commit 81c406b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2021 Expedia Inc.
Copyright (C) 2013-2024 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,6 +74,11 @@ default <T> Optional<T> getIfAvailable(String key, Class<T> clazz) {
* @return returns the netty executor which started handling the current request
*/
Executor executor();

/**
* Removes all the stored items from this context.
*/
void clear();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public Optional<InetSocketAddress> clientAddress() {
public Executor executor() {
return Runnable::run;
}

@Override
public void clear() {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2023 Expedia Inc.
Copyright (C) 2013-2024 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,16 +25,26 @@ import java.util.concurrent.Executor
* (because the call was not made within an interceptor chain).
*/
object DummyContext : HttpInterceptor.Context {
override fun add(key: String, value: Any) {
override fun add(
key: String,
value: Any,
) {
// Empty by design
}

@Deprecated("Deprecated in Java", ReplaceWith("getIfAvailable"))
override fun <T : Any> get(key: String, clazz: Class<T>): T? = null
override fun <T : Any> get(
key: String,
clazz: Class<T>,
): T? = null

override fun isSecure(): Boolean = false

override fun clientAddress(): Optional<InetSocketAddress> = Optional.empty()

override fun executor(): Executor = Executor { it.run() }

override fun clear() {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2023 Expedia Inc.
Copyright (C) 2013-2024 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,6 @@ import java.util.Optional
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executor


/**
* ConcurrentHashMap backed implementation of HttpInterceptor.Context.
*
Expand All @@ -34,26 +33,34 @@ class HttpInterceptorContext(
private val secure: Boolean,
private val clientAddress: InetSocketAddress?,
private val executor: Executor?,
override val timers: ContextualTimers? = null
override val timers: ContextualTimers? = null,
) : HttpInterceptor.Context, TimeMeasurable {
// This may seem redundant but it allows Executor to be a lambda without needing to set `timers`.
constructor(secure: Boolean, clientAddress: InetSocketAddress?, executor: Executor?) : this(secure, clientAddress, executor, null)

private val context = ConcurrentHashMap<String, Any>()

override fun add(key: String, value: Any) {
override fun add(
key: String,
value: Any,
) {
context[key] = value
}

@Deprecated("Deprecated in Java")
override fun <T> get(key: String, clazz: Class<T>): T = context[key] as T
override fun <T> get(
key: String,
clazz: Class<T>,
): T = context[key] as T

override fun isSecure() = secure

override fun clientAddress() = Optional.ofNullable(clientAddress)

override fun executor(): Executor? = executor

override fun clear() = context.clear()

companion object {
// Visible for testing
@Deprecated("use the constructor instead")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2023 Expedia Inc.
Copyright (C) 2013-2024 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -278,6 +278,8 @@ protected void hookOnCancel() {
protected void hookFinally(SignalType type) {
timers.stopTiming(RESPONSE_PROCESSING);
timers.stopTiming(REQUEST_PROCESSING);

context.clear();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2021 Expedia Inc.
Copyright (C) 2013-2024 Expedia Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -81,6 +81,11 @@ public Optional<InetSocketAddress> clientAddress() {
public Executor executor() {
return this.executor;
}

@Override
public void clear() {
context.clear();
}
}

}

0 comments on commit 81c406b

Please sign in to comment.