Skip to content

Commit

Permalink
do not set dispatcher for file directives and remove file-io-dispatch…
Browse files Browse the repository at this point in the history
…er config akka#1879
  • Loading branch information
shkoder committed Dec 5, 2018
1 parent 13add16 commit a0769fc
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 50 deletions.
Expand Up @@ -18,7 +18,7 @@ private[http] final case class RoutingSettingsImpl(
rangeCoalescingThreshold: Long,
decodeMaxBytesPerChunk: Int,
decodeMaxSize: Long,
fileIODispatcher: String) extends akka.http.scaladsl.settings.RoutingSettings {
fileIODispatcher: String = "") extends akka.http.scaladsl.settings.RoutingSettings {

override def productPrefix = "RoutingSettings"
}
Expand All @@ -31,6 +31,5 @@ object RoutingSettingsImpl extends SettingsCompanion[RoutingSettingsImpl]("akka.
c getInt "range-count-limit",
c getBytes "range-coalescing-threshold",
c getIntBytes "decode-max-bytes-per-chunk",
c getPossiblyInfiniteBytes "decode-max-size",
c getString "file-io-dispatcher")
c getPossiblyInfiniteBytes "decode-max-size")
}
Expand Up @@ -20,6 +20,7 @@ abstract class RoutingSettings private[akka] () { self: RoutingSettingsImpl ⇒
def getRangeCountLimit: Int
def getRangeCoalescingThreshold: Long
def getDecodeMaxBytesPerChunk: Int
@deprecated("binary compatibility method. Use `akka.stream.materializer.blocking-io-dispatcher` to configure the dispatcher")
def getFileIODispatcher: String

def withVerboseErrorMessages(verboseErrorMessages: Boolean): RoutingSettings = self.copy(verboseErrorMessages = verboseErrorMessages)
Expand All @@ -29,7 +30,8 @@ abstract class RoutingSettings private[akka] () { self: RoutingSettingsImpl ⇒
def withRangeCoalescingThreshold(rangeCoalescingThreshold: Long): RoutingSettings = self.copy(rangeCoalescingThreshold = rangeCoalescingThreshold)
def withDecodeMaxBytesPerChunk(decodeMaxBytesPerChunk: Int): RoutingSettings = self.copy(decodeMaxBytesPerChunk = decodeMaxBytesPerChunk)
def withDecodeMaxSize(decodeMaxSize: Long): RoutingSettings = self.copy(decodeMaxSize = decodeMaxSize)
def withFileIODispatcher(fileIODispatcher: String): RoutingSettings = self.copy(fileIODispatcher = fileIODispatcher)
@deprecated("binary compatibility method. Use `akka.stream.materializer.blocking-io-dispatcher` to configure the dispatcher")
def withFileIODispatcher(fileIODispatcher: String): RoutingSettings = self
}

object RoutingSettings extends SettingsCompanion[RoutingSettings] {
Expand Down
Expand Up @@ -20,6 +20,7 @@ abstract class RoutingSettings private[akka] () extends akka.http.javadsl.settin
def rangeCoalescingThreshold: Long
def decodeMaxBytesPerChunk: Int
def decodeMaxSize: Long
@deprecated("binary compatibility method. Use `akka.stream.materializer.blocking-io-dispatcher` to configure the dispatcher")
def fileIODispatcher: String

/* Java APIs */
Expand All @@ -30,6 +31,7 @@ abstract class RoutingSettings private[akka] () extends akka.http.javadsl.settin
def getRangeCoalescingThreshold: Long = rangeCoalescingThreshold
def getDecodeMaxBytesPerChunk: Int = decodeMaxBytesPerChunk
def getDecodeMaxSize: Long = decodeMaxSize
@deprecated("binary compatibility method. Use `akka.stream.materializer.blocking-io-dispatcher` to configure the dispatcher")
def getFileIODispatcher: String = fileIODispatcher

override def withVerboseErrorMessages(verboseErrorMessages: Boolean): RoutingSettings = self.copy(verboseErrorMessages = verboseErrorMessages)
Expand All @@ -39,7 +41,8 @@ abstract class RoutingSettings private[akka] () extends akka.http.javadsl.settin
override def withRangeCoalescingThreshold(rangeCoalescingThreshold: Long): RoutingSettings = self.copy(rangeCoalescingThreshold = rangeCoalescingThreshold)
override def withDecodeMaxBytesPerChunk(decodeMaxBytesPerChunk: Int): RoutingSettings = self.copy(decodeMaxBytesPerChunk = decodeMaxBytesPerChunk)
override def withDecodeMaxSize(decodeMaxSize: Long): RoutingSettings = self.copy(decodeMaxSize = decodeMaxSize)
override def withFileIODispatcher(fileIODispatcher: String): RoutingSettings = self.copy(fileIODispatcher = fileIODispatcher)
@deprecated("binary compatibility method. Use `akka.stream.materializer.blocking-io-dispatcher` to configure the dispatcher")
override def withFileIODispatcher(fileIODispatcher: String): RoutingSettings = self
}

object RoutingSettings extends SettingsCompanion[RoutingSettings] {
Expand Down
Expand Up @@ -23,7 +23,6 @@ public void testCreateWithActorSystem() {
" range-count-limit = 16\n" +
" decode-max-bytes-per-chunk = 1m\n" +
" decode-max-size = 8m\n" +
" file-io-dispatcher = \"test-only\"\n" +
"}";
Config config = ConfigFactory.parseString(testConfig);
ActorSystem sys = ActorSystem.create("test", config);
Expand Down
Expand Up @@ -20,7 +20,6 @@ class SettingsEqualitySpec extends WordSpec with Matchers {
range-count-limit = 16
decode-max-bytes-per-chunk = 1m
decode-max-size = 8m
file-io-dispatcher = ${akka.stream.blocking-io-dispatcher}
}
""").withFallback(ConfigFactory.load).resolve

Expand Down
4 changes: 0 additions & 4 deletions akka-http/src/main/resources/reference.conf
Expand Up @@ -45,8 +45,4 @@ akka.http.routing {
# This limit (like max-content-length) can be overridden on a case-by-case basis using the
# withSizeLimit directive.
decode-max-size = 8m

# Fully qualified config path which holds the dispatcher configuration
# to be used by FlowMaterialiser when creating Actors for IO operations.
file-io-dispatcher = ${akka.stream.blocking-io-dispatcher}
}
Expand Up @@ -9,7 +9,6 @@ import java.io.File
import java.net.{ URI, URL }

import akka.http.javadsl.{ marshalling, model }
import akka.stream.ActorAttributes
import akka.stream.scaladsl.{ FileIO, StreamConverters }

import scala.annotation.tailrec
Expand Down Expand Up @@ -66,11 +65,8 @@ trait FileAndResourceDirectives {
if (file.isFile && file.canRead)
conditionalFor(file.length, file.lastModified) {
if (file.length > 0) {
withRangeSupportAndPrecompressedMediaTypeSupportAndExtractSettings { settings
complete {
HttpEntity.Default(contentType, file.length,
FileIO.fromPath(file.toPath).withAttributes(ActorAttributes.dispatcher(settings.fileIODispatcher)))
}
withRangeSupportAndPrecompressedMediaTypeSupport {
complete(HttpEntity.Default(contentType, file.length, FileIO.fromPath(file.toPath)))
}
} else complete(HttpEntity.Empty)
}
Expand Down Expand Up @@ -107,12 +103,8 @@ trait FileAndResourceDirectives {
case Some(ResourceFile(url, length, lastModified))
conditionalFor(length, lastModified) {
if (length > 0) {
withRangeSupportAndPrecompressedMediaTypeSupportAndExtractSettings { settings
complete {
HttpEntity.Default(contentType, length,
StreamConverters.fromInputStream(() url.openStream())
.withAttributes(ActorAttributes.dispatcher(settings.fileIODispatcher))) // TODO is this needed? It already uses `val inputStreamSource = name("inputStreamSource") and IODispatcher`
}
withRangeSupportAndPrecompressedMediaTypeSupport {
complete(HttpEntity.Default(contentType, length, StreamConverters.fromInputStream(() url.openStream())))
}
} else complete(HttpEntity.Empty)
}
Expand Down Expand Up @@ -210,10 +202,9 @@ trait FileAndResourceDirectives {
}

object FileAndResourceDirectives extends FileAndResourceDirectives {
private val withRangeSupportAndPrecompressedMediaTypeSupportAndExtractSettings =
private val withRangeSupportAndPrecompressedMediaTypeSupport =
RangeDirectives.withRangeSupport &
CodingDirectives.withPrecompressedMediaTypeSupport &
BasicDirectives.extractSettings
CodingDirectives.withPrecompressedMediaTypeSupport

private def withTrailingSlash(path: String): String = if (path endsWith "/") path else path + '/'

Expand Down
Expand Up @@ -17,7 +17,7 @@ See also @ref[withSettings](withSettings.md) or @ref[extractSettings](extractSet
## Example

Scala
: @@snip [BasicDirectivesExamplesSpec.scala]($test$/scala/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala) { #withSettings-0 }
: @@snip [BasicDirectivesExamplesSpec.scala]($test$/scala/docs/http/scaladsl/server/directives/BasicDirectivesExamplesSpec.scala) { #mapSettings-examples }

Java
: @@snip [BasicDirectivesExamplesTest.java]($test$/java/docs/http/javadsl/server/directives/BasicDirectivesExamplesTest.java) { #mapSettings }
Expand Up @@ -426,27 +426,18 @@ public void testWithSettings() {
final RoutingSettings special =
RoutingSettings
.create(system().settings().config())
.withFileIODispatcher("special-io-dispatcher");

final Route sample = path("sample", () -> {
// internally uses the configured fileIODispatcher:
// ContentTypes.APPLICATION_JSON, source
final Source<ByteString, Object> source =
FileIO.fromPath(Paths.get("example.json"))
.mapMaterializedValue(completionStage -> (Object) completionStage);
return complete(
HttpResponse.create()
.withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, source))
);
});
.withFileGetConditional(false);

// internally uses fileGetConditional setting
final Route sample = path("sample", () -> getFromFile("example.json"));

final Route route = get(() ->
Directives.concat(
pathPrefix("special", () ->
// `special` file-io-dispatcher will be used to read the file
// ETag/`If-Modified-Since` disabled
withSettings(special, () -> sample)
),
sample // default file-io-dispatcher will be used to read the file
sample // ETag/`If-Modified-Since` enabled
)
);

Expand Down
Expand Up @@ -184,24 +184,21 @@ class BasicDirectivesExamplesSpec extends RoutingSpec {
}
"withSettings-0" in compileOnlySpec {
//#withSettings-0
val special = RoutingSettings(system).withFileIODispatcher("special-io-dispatcher")
val special = RoutingSettings(system).withFileGetConditional(false)

def sample() =
path("sample") {
complete {
// internally uses the configured fileIODispatcher:
val source = FileIO.fromPath(Paths.get("example.json"))
HttpResponse(entity = HttpEntity(ContentTypes.`application/json`, source))
}
// internally uses fileGetConditional setting
getFromFile("example.json")
}

val route =
get {
pathPrefix("special") {
withSettings(special) {
sample() // `special` file-io-dispatcher will be used to read the file
sample() // ETag/`If-Modified-Since` disabled
}
} ~ sample() // default file-io-dispatcher will be used to read the file
} ~ sample() // ETag/`If-Modified-Since` enabled
}

// tests:
Expand Down

0 comments on commit a0769fc

Please sign in to comment.