-
Notifications
You must be signed in to change notification settings - Fork 411
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
Further optimise zio.http.Body.FileBody.asStream
code
#3234
Conversation
ae26d2b
to
d9c9624
Compare
e439994
to
8c69633
Compare
} | ||
(fs, size) = r | ||
} yield ZStream | ||
.repeatZIOOption[Any, Throwable, Chunk[Byte]] { |
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.
} yield bytes | ||
Exit.succeed { | ||
ZStream.repeatZIOChunkOption { | ||
ZIO.suspendSucceed { |
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.
@kyri-petrou Are we sure this ZIO.suspendSucceed
and the ZIO.attempt(fs.close())
will be run on a blocking thread? 🤔
f7a737c
to
96653a6
Compare
96653a6
to
ab1fc3d
Compare
Exit.succeed { | ||
// Optimised for our needs version of `ZIO.repeatZIOChunkOption` | ||
ZStream | ||
.unfoldChunkZIO(read)(_.map(_.map(_ -> read))) |
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.
@kyri-petrou Funnily enough, if we were managing to find a way to write a recursive type in Scala,
- we wouldn't need this ugly
(_.map(_.map(_ -> read))
- we could write this:
type Read = Task[Option[(Chunk[Byte], Read)]]
val read: Read =
ZIO.suspendSucceed {
try {
val buffer = new Array[Byte](size)
val len = fs.read(buffer)
if (len > 0) Exit.succeed(Some(Chunk.fromArray(buffer.slice(0, len) -> read)))
else Exit.none
} catch {
case e: Throwable => Exit.fail(e)
}
}
Exit.succeed {
ZStream
.unfoldChunkZIO(read)(ZIO.identityFn)
.ensuring(ZIO.attempt(fs.close()).ignoreLogged)
}
😍
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 can imagine, that there is a nice way to do this for Scala 3. But I think we are unable to find a nice way for Scala 2
Following #3215