-
Notifications
You must be signed in to change notification settings - Fork 426
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
Simplified and refactored Netty-ReactiveStreams integration #3636
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
70d44ac
SimpleSubscriber simplified even more
ghik 9f2883e
FileWriterSubscriber simplified
ghik 79ecbe7
contentLength in NettyRequestBody changed to Long
ghik 9e6ddea
accepting explicit RunAsync in InputStreamPublisher
ghik 6bacd31
removed accidentally committed change
ghik 58e3b92
fixed bad import
ghik e11f2d4
added comment about Id in InputStreamPublisher
ghik 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
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
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
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
14 changes: 14 additions & 0 deletions
14
server/netty-server/src/main/scala/sttp/tapir/server/netty/internal/RunAsync.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 |
---|---|---|
@@ -1,5 +1,19 @@ | ||
package sttp.tapir.server.netty.internal | ||
|
||
import scala.concurrent.Future | ||
|
||
trait RunAsync[F[_]] { | ||
def apply(f: => F[Unit]): Unit | ||
} | ||
object RunAsync { | ||
type Id[A] = A | ||
|
||
final val Id: RunAsync[Id] = new RunAsync[Id] { | ||
override def apply(f: => Id[Unit]): Unit = f | ||
} | ||
|
||
final val Future: RunAsync[Future] = new RunAsync[Future] { | ||
override def apply(f: => Future[Unit]): Unit = | ||
f: Unit | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,15 +3,22 @@ package sttp.tapir.server.netty.internal.reactivestreams | |
import io.netty.buffer.Unpooled | ||
import io.netty.handler.codec.http.{DefaultHttpContent, HttpContent} | ||
import org.reactivestreams.{Publisher, Subscriber, Subscription} | ||
import sttp.monad.MonadError | ||
import sttp.monad.syntax._ | ||
import sttp.tapir.InputStreamRange | ||
import sttp.tapir.server.netty.internal.RunAsync | ||
|
||
import java.io.InputStream | ||
import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong} | ||
import scala.util.Try | ||
import sttp.monad.MonadError | ||
import sttp.monad.syntax._ | ||
|
||
class InputStreamPublisher[F[_]](range: InputStreamRange, chunkSize: Int)(implicit monad: MonadError[F]) extends Publisher[HttpContent] { | ||
class InputStreamPublisher[F[_]]( | ||
range: InputStreamRange, | ||
chunkSize: Int, | ||
runAsync: RunAsync[F] | ||
)(implicit | ||
monad: MonadError[F] | ||
) extends Publisher[HttpContent] { | ||
override def subscribe(subscriber: Subscriber[_ >: HttpContent]): Unit = { | ||
if (subscriber == null) throw new NullPointerException("Subscriber cannot be null") | ||
val subscription = new InputStreamSubscription(subscriber, range, chunkSize) | ||
|
@@ -46,7 +53,7 @@ class InputStreamPublisher[F[_]](range: InputStreamRange, chunkSize: Int)(implic | |
case _ => chunkSize | ||
} | ||
|
||
val _ = monad | ||
runAsync(monad | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: I think we still should add a comment that in case of |
||
.blocking( | ||
stream.readNBytes(expectedBytes) | ||
) | ||
|
@@ -69,11 +76,10 @@ class InputStreamPublisher[F[_]](range: InputStreamRange, chunkSize: Int)(implic | |
} | ||
} | ||
.handleError { | ||
case e => { | ||
case e => | ||
val _ = Try(stream.close()) | ||
monad.unit(subscriber.onError(e)) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.
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.
out of curiosity: anything better in
final val Id
overobject Id extends RunAsync[Id]
?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.
Hmm, no strong opinions.
It arguably generates somewhat simpler bytecode, i.e.
object
object
(there's an anonymous class but it will probably be compiled to a lambda so it won't have a classfile)It also works more naturally with type inference:
val
, type ofId
will be inferred asRunAsync[Id]
(unless explicitly requested to be typed asId.type
)object
, it will be inferred asId.type
, with API potentially extended overRunAsync[Id]
Neither the laziness nor introducing a subtype was my intention, so a
val
is much closer to expressing exactly what I want, which is just having a plain implementation ofRunAsync
.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.
sounds good, thanks :)