-
Notifications
You must be signed in to change notification settings - Fork 427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize parsing headers for pekko-http and akka-http #3575
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3da88b5
Cache ContenType to avoid excessive parsing for Pekko responses
kciesielski bb4ea8d
Filter headers before parsing
kciesielski faada28
Apply optimizations to akka-http
kciesielski 04e9804
Add missing class for akka-http
kciesielski 2415d1d
Don't cache MediaType with params
kciesielski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
29 changes: 29 additions & 0 deletions
29
server/akka-http-server/src/main/scala/sttp/tapir/server/akkahttp/ContentTypeCache.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,29 @@ | ||
package sttp.tapir.server.akkahttp | ||
|
||
import akka.http.scaladsl.model.ContentType | ||
import scala.collection.concurrent.TrieMap | ||
|
||
/** Pekko-specific ConentType has to be created if an endpoint overrides it, but we want to reduce overhead of the expensive | ||
* ContentType.parse operation if possible. Parsing may also happen for cases not listed explictly in | ||
* PekkoToResponseBody.formatToContentType. This cache doesn't have to save atomically, because the worst case scenario is that we parse he | ||
* same header a few times before it's saved. The cache is not cleared, because the number of different content types is limited and the | ||
* cache is not expected to grow too much. The only exception is when there is a boundary in the header, but in such situation the endpoint | ||
* contentType shouldn't be overriden. Just in case this happens, we limit the cache size. | ||
*/ | ||
private[akkahttp] object ContentTypeCache { | ||
private val cache = TrieMap[String, ContentType]() | ||
private val Limit = 100 | ||
|
||
def getOrParse(headerValue: String): ContentType = { | ||
cache.get(headerValue) match { | ||
case Some(contentType) => | ||
contentType | ||
case None => | ||
val contentType = | ||
ContentType.parse(headerValue).getOrElse(throw new IllegalArgumentException(s"Cannot parse content type: $headerValue")) | ||
// We don't want to fill the cache with parameterized media types (BTW charset does not appear in params) | ||
val _ = if (cache.size <= Limit && contentType.mediaType.params.isEmpty) cache.putIfAbsent(headerValue, contentType) | ||
contentType | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
server/pekko-http-server/src/main/scala/sttp/tapir/server/pekkohttp/ContentTypeCache.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,29 @@ | ||
package sttp.tapir.server.pekkohttp | ||
|
||
import org.apache.pekko.http.scaladsl.model.ContentType | ||
import scala.collection.concurrent.TrieMap | ||
|
||
/** Pekko-specific ConentType has to be created if an endpoint overrides it, but we want to reduce overhead of the expensive | ||
* ContentType.parse operation if possible. Parsing may also happen for cases not listed explictly in | ||
* PekkoToResponseBody.formatToContentType. This cache doesn't have to save atomically, because the worst case scenario is that we parse he | ||
* same header a few times before it's saved. The cache is not cleared, because the number of different content types is limited and the | ||
* cache is not expected to grow too much. The only exception is when there is a boundary in the header, but in such situation the endpoint | ||
* contentType shouldn't be overriden. Just in case this happens, we limit the cache size. | ||
*/ | ||
private[pekkohttp] object ContentTypeCache { | ||
private val cache = TrieMap[String, ContentType]() | ||
private val Limit = 100 | ||
|
||
def getOrParse(headerValue: String): ContentType = { | ||
cache.get(headerValue) match { | ||
case Some(contentType) => | ||
contentType | ||
case None => | ||
val contentType = | ||
ContentType.parse(headerValue).getOrElse(throw new IllegalArgumentException(s"Cannot parse content type: $headerValue")) | ||
// We don't want to fill the cache with parameterized media types (BTW charset does not appear in params) | ||
val _ = if (cache.size <= Limit && contentType.mediaType.params.isEmpty) cache.putIfAbsent(headerValue, contentType) | ||
contentType | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if can get some "wild" media types, e.g. with some parameters, which could fill our cache with junk. We have a list of (currently) 1208 mime types in
mimeByExtension.txt
, but it has some very weird media types, and parsing them upfront would take significant time. Or maybe we could cache only ifcontentType.mediaType.params.isEmpty
? (well .. maybe except for the charset)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of storing only values which have empty params (except the charset), this should significantly limit the cases where the cache is not useful after being filled with a lot of types.