Skip to content

Commit

Permalink
finagle-base-http: change default header map impl
Browse files Browse the repository at this point in the history
Problem / Solution

This patch changes the default header map implementation
for finagle-http. This is the remainder of this patch:
https://github.com/twitter/finagle/pull/805/files

Differential Revision: https://phabricator.twitter.biz/D394981
  • Loading branch information
Ruben Oanta authored and jenkins committed Nov 12, 2019
1 parent a9b68da commit 597e781
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
Expand Up @@ -3,5 +3,5 @@ package com.twitter.finagle.http.headers
import com.twitter.finagle.http.HeaderMap

object DefaultHeaderMap {
def apply(headers: (String, String)*): HeaderMap = HashBackedHeaderMap(headers: _*)
def apply(headers: (String, String)*): HeaderMap = JTreeMapBackedHeaderMap(headers: _*)
}
Expand Up @@ -2,7 +2,6 @@ package com.twitter.finagle.http.headers

import com.twitter.finagle.http.HeaderMap
import scala.annotation.tailrec
import java.util.function.BiConsumer

/**
* Mutable, thread-safe [[HeaderMap]] implementation, backed by
Expand All @@ -21,15 +20,16 @@ final private class JTreeMapBackedHeaderMap extends HeaderMap {
private[this] val underlying: java.util.TreeMap[String, Header.Root] =
new java.util.TreeMap[String, Header.Root](JTreeMapBackedHeaderMap.sharedComparator)

private def foreachConsumer[U](f: ((String, String)) => U): BiConsumer[String, Header.Root] =
new BiConsumer[String, Header.Root]() {
def accept(key: String, header: Header.Root): Unit = header.iterator.foreach(
nv => f((nv.name, nv.value))
)
override def foreach[U](f: ((String, String)) => U): Unit = {
// We need to copy to a new iterator before calling the
// user defined functions `f` since they can add/remove
// to the header map which would invalidate the iterator
// on the underlying TreeMap and cause a
// ConcurrentModificationException.
val iter = copyHeaders
iter.foreach { root =>
root.iterator.foreach(nv => f((nv.name, nv.value)))
}

override def foreach[U](f: ((String, String)) => U): Unit = underlying.synchronized {
underlying.forEach(foreachConsumer(f))
}

// ---- HeaderMap -----
Expand Down

0 comments on commit 597e781

Please sign in to comment.