-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finagle/finagle-core: Introduce ClearBroadcastContextFilter
Problem We want to use `MarshalledContext.retainIds` to clear the broadcast context except for certain keys. Solution Create a filter `ClearBroadcastContextFilter`. Differential Revision: https://phabricator.twitter.biz/D1179110
- Loading branch information
Showing
5 changed files
with
86 additions
and
4 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
36 changes: 36 additions & 0 deletions
36
finagle-core/src/main/scala/com/twitter/finagle/filter/ClearBroadcastContextFilter.scala
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,36 @@ | ||
package com.twitter.finagle.filter | ||
|
||
import com.twitter.finagle.context.Contexts | ||
import com.twitter.finagle._ | ||
import com.twitter.util.Future | ||
|
||
/** | ||
* ClearBroadcastContextFilter clears the broadcast context of all context keys except those specified. | ||
*/ | ||
private[twitter] object ClearBroadcastContextFilter { | ||
val role = Stack.Role("ClearBroadcastContextFilter") | ||
|
||
/** | ||
* Creates a [[com.twitter.finagle.Stackable]] [[com.twitter.finagle.Filter]] that clears the | ||
* broadcast context of all context keys except those specified. | ||
*/ | ||
def module[Req, Rep]( | ||
retainKeys: Set[Contexts.broadcast.Key[_]] | ||
): Stackable[ServiceFactory[Req, Rep]] = | ||
new Stack.Module0[ServiceFactory[Req, Rep]] { | ||
val role = ClearBroadcastContextFilter.role | ||
val description = | ||
s"Clears the broadcast context of all keys except: ${retainKeys.mkString(",")}" | ||
val lookupIds = retainKeys.map(_.lookupId) | ||
def make(next: ServiceFactory[Req, Rep]): ServiceFactory[Req, Rep] = { | ||
val filter = new SimpleFilter[Req, Rep] { | ||
def apply(request: Req, service: Service[Req, Rep]): Future[Rep] = { | ||
Contexts.broadcast.retainIds(lookupIds) { | ||
service(request) | ||
} | ||
} | ||
} | ||
filter.andThen(next) | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
finagle-core/src/test/scala/com/twitter/finagle/filter/ClearBroadcastContextFilterTest.scala
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,44 @@ | ||
package com.twitter.finagle.filter | ||
|
||
import com.twitter.conversions.DurationOps._ | ||
import com.twitter.finagle._ | ||
import com.twitter.finagle.context.Contexts | ||
import com.twitter.finagle.context.Deadline | ||
import com.twitter.finagle.context.Requeues | ||
import com.twitter.finagle.stack.nilStack | ||
import com.twitter.io.Buf | ||
import com.twitter.util.Await | ||
import com.twitter.util.Future | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class ClearBroadcastContextFilterTest extends AnyFunSuite { | ||
|
||
test("clears context except for the configured keys") { | ||
val clearContextFilter = | ||
ClearBroadcastContextFilter.module[Unit, Set[String]](retainKeys = Set(Deadline)) | ||
|
||
val svcModule = new Stack.Module0[ServiceFactory[Unit, Set[String]]] { | ||
val role = Stack.Role("svcModule") | ||
val description = "" | ||
|
||
def make(next: ServiceFactory[Unit, Set[String]]) = | ||
ServiceFactory.const( | ||
Service.mk[Unit, Set[String]](_ => | ||
Future.value(Contexts.broadcast | ||
.marshal().map { | ||
case (k, v) => | ||
Buf.Utf8.unapply(k).get | ||
}.toSet))) | ||
} | ||
|
||
val factory = new StackBuilder[ServiceFactory[Unit, Set[String]]](nilStack[Unit, Set[String]]) | ||
.push(svcModule) | ||
.push(clearContextFilter) | ||
.make(Stack.Params.empty) | ||
|
||
val svc: Service[Unit, Set[String]] = Await.result(factory(), 1.second) | ||
Contexts.broadcast.let(Requeues, Requeues(5), Deadline, Deadline.ofTimeout(5.seconds)) { | ||
assert(Await.result(svc(()), 1.second) == Set(Deadline.id)) | ||
} | ||
} | ||
} |