Skip to content

Commit

Permalink
Merge branch 'main' into optimise/handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
guizmaii authored Dec 3, 2024
2 parents ab6e100 + 547975e commit e5d11f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
36 changes: 21 additions & 15 deletions zio-http/shared/src/main/scala/zio/http/Body.scala
Original file line number Diff line number Diff line change
Expand Up @@ -567,21 +567,27 @@ object Body {

override def asStream(implicit trace: Trace): ZStream[Any, Throwable, Byte] =
ZStream.unwrap {
for {
file <- ZIO.attempt(file)
fs <- ZIO.attemptBlocking(new FileInputStream(file))
size <- ZIO.attemptBlocking(Math.min(chunkSize.toLong, file.length()).toInt)
} yield ZStream
.repeatZIOOption[Any, Throwable, Chunk[Byte]] {
for {
buffer <- ZIO.succeed(new Array[Byte](size))
len <- ZIO.attemptBlocking(fs.read(buffer)).mapError(Some(_))
bytes <-
if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len)))
else ZIO.fail(None)
} yield bytes
}
.ensuring(ZIO.attemptBlocking(fs.close()).ignoreLogged)
ZIO.blocking {
for {
r <- ZIO.attempt {
val fs = new FileInputStream(file)
val size = Math.min(chunkSize.toLong, file.length()).toInt

(fs, size)
}
(fs, size) = r
} yield ZStream
.repeatZIOOption[Any, Throwable, Chunk[Byte]] {
for {
buffer <- ZIO.succeed(new Array[Byte](size))
len <- ZIO.attempt(fs.read(buffer)).mapError(Some(_))
bytes <-
if (len > 0) ZIO.succeed(Chunk.fromArray(buffer.slice(0, len)))
else ZIO.fail(None)
} yield bytes
}
.ensuring(ZIO.attempt(fs.close()).ignoreLogged)
}
}.flattenChunks

override def contentType(newContentType: Body.ContentType): Body = copy(contentType = Some(newContentType))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,18 +419,21 @@ private[codec] object EncoderDecoder {
private def decodeBody(config: CodecConfig, body: Body, inputs: Array[Any])(implicit
trace: Trace,
): Task[Unit] = {
val codecs = flattened.content
val isNonMultiPart = inputs.length < 2
if (isNonMultiPart) {
val codecs = flattened.content

if (inputs.length < 2) {
// non multi-part
codecs.headOption.map { codec =>
// noinspection SimplifyUnlessInspection
if (codecs.isEmpty) ZIO.unit
else {
val codec = codecs.head
codec
.decodeFromBody(body, config)
.mapBoth(
{ err => HttpCodecError.MalformedBody(err.getMessage(), Some(err)) },
{ err => HttpCodecError.MalformedBody(err.getMessage, Some(err)) },
result => inputs(0) = result,
)
}.getOrElse(ZIO.unit)
}
} else {
// multi-part
decodeForm(body.asMultipartFormStream, inputs, config) *> check(inputs)
Expand Down

2 comments on commit e5d11f4

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 : Performance Benchmarks (SimpleEffectBenchmarkServer)

concurrency: 256
requests/sec: 349362

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 : Performance Benchmarks (PlainTextBenchmarkServer)

concurrency: 256
requests/sec: 352521

Please sign in to comment.