-
Notifications
You must be signed in to change notification settings - Fork 26
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
Support for fs2 2.3.0 and scala 2.13 #44
base: series/0.5
Are you sure you want to change the base?
Changes from 3 commits
ddb2fa7
77f28e8
645c57a
5c2632c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
language : scala | ||
|
||
scala: | ||
- 2.11.12 | ||
- 2.12.6 | ||
- 2.12.11 | ||
- 2.13.0 | ||
|
||
dist: trusty | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
package spinoco.fs2.http | ||
|
||
import java.nio.channels.AsynchronousChannelGroup | ||
|
||
import cats.effect.{Concurrent, ConcurrentEffect, ContextShift, Sync, Timer} | ||
import java.util.concurrent.TimeUnit | ||
|
||
import cats.Applicative | ||
import javax.net.ssl.SSLContext | ||
import cats.effect._ | ||
import fs2._ | ||
import fs2.concurrent.SignallingRef | ||
import fs2.io.tcp.Socket | ||
import fs2.io.tcp.{Socket, SocketGroup} | ||
import fs2.io.tls.TLSContext | ||
import scodec.{Codec, Decoder, Encoder} | ||
import spinoco.fs2.http.internal.{addressForRequest, clientLiftToSecure, readWithTimeout} | ||
import spinoco.fs2.http.sse.{SSEDecoder, SSEEncoding} | ||
|
@@ -17,7 +17,6 @@ import spinoco.protocol.http.header._ | |
import spinoco.protocol.mime.MediaType | ||
import spinoco.protocol.http.{HttpRequestHeader, HttpResponseHeader} | ||
|
||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.duration._ | ||
|
||
|
||
|
@@ -114,12 +113,10 @@ trait HttpClient[F[_]] { | |
def apply[F[_] : ConcurrentEffect : ContextShift : Timer]( | ||
requestCodec : Codec[HttpRequestHeader] | ||
, responseCodec : Codec[HttpResponseHeader] | ||
, sslExecutionContext: => ExecutionContext | ||
, sslContext : => SSLContext | ||
)(implicit AG: AsynchronousChannelGroup):F[HttpClient[F]] = Sync[F].delay { | ||
lazy val sslCtx = sslContext | ||
lazy val sslS = sslExecutionContext | ||
|
||
)( | ||
socketGroup: SocketGroup | ||
, tlsContext: TLSContext | ||
):F[HttpClient[F]] = Sync[F].delay { | ||
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. No need for Sync, I think Applicative will do |
||
new HttpClient[F] { | ||
def request( | ||
request: HttpRequest[F] | ||
|
@@ -128,10 +125,10 @@ trait HttpClient[F[_]] { | |
, timeout: Duration | ||
): Stream[F, HttpResponse[F]] = { | ||
Stream.eval(addressForRequest[F](request.scheme, request.host)).flatMap { address => | ||
Stream.resource(io.tcp.client[F](address)) | ||
.evalMap { socket => | ||
if (!request.isSecure) Applicative[F].pure(socket) | ||
else clientLiftToSecure[F](sslS, sslCtx)(socket, request.host) | ||
Stream.resource(socketGroup.client(address)) | ||
.flatMap { socket => | ||
if (!request.isSecure) Stream.emit(socket) | ||
else Stream.resource(clientLiftToSecure[F](tlsContext)(socket, request.host)) | ||
} | ||
.flatMap { impl.request[F](request, chunkSize, maxResponseHeaderSize, timeout, requestCodec, responseCodec ) }} | ||
} | ||
|
@@ -143,7 +140,7 @@ trait HttpClient[F[_]] { | |
, chunkSize: Int | ||
, maxFrameSize: Int | ||
): Stream[F, Option[HttpResponseHeader]] = | ||
WebSocket.client(request,pipe,maxResponseHeaderSize,chunkSize,maxFrameSize, requestCodec, responseCodec, sslS, sslCtx) | ||
WebSocket.client(request,pipe,maxResponseHeaderSize,chunkSize,maxFrameSize, requestCodec, responseCodec)(socketGroup, tlsContext) | ||
|
||
|
||
def sse[A : SSEDecoder](rq: HttpRequest[F], maxResponseHeaderSize: Int, chunkSize: Int): Stream[F, A] = | ||
|
@@ -174,7 +171,7 @@ trait HttpClient[F[_]] { | |
timeout match { | ||
case fin: FiniteDuration => | ||
eval(clock.realTime(TimeUnit.MILLISECONDS)).flatMap { start => | ||
HttpRequest.toStream(request, requestCodec).to(socket.writes(Some(fin))).last.onFinalize(socket.endOfOutput).flatMap { _ => | ||
HttpRequest.toStream(request, requestCodec).through(socket.writes(Some(fin))).last.onFinalize(socket.endOfOutput).flatMap { _ => | ||
eval(SignallingRef[F, Boolean](true)).flatMap { timeoutSignal => | ||
eval(clock.realTime(TimeUnit.MILLISECONDS)).flatMap { sent => | ||
val remains = fin - (sent - start).millis | ||
|
@@ -186,7 +183,7 @@ trait HttpClient[F[_]] { | |
}}}} | ||
|
||
case _ => | ||
HttpRequest.toStream(request, requestCodec).to(socket.writes(None)).last.onFinalize(socket.endOfOutput).flatMap { _ => | ||
HttpRequest.toStream(request, requestCodec).through(socket.writes(None)).last.onFinalize(socket.endOfOutput).flatMap { _ => | ||
socket.reads(chunkSize, None) through HttpResponse.fromStream[F](maxResponseHeaderSize, responseCodec) | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package spinoco.fs2.http | ||
|
||
import java.net.InetSocketAddress | ||
import java.nio.channels.AsynchronousChannelGroup | ||
|
||
import cats.effect.{ConcurrentEffect, Sync, Timer} | ||
import cats.effect.{ConcurrentEffect, ContextShift, Sync, Timer} | ||
import cats.syntax.all._ | ||
import fs2._ | ||
import fs2.concurrent.SignallingRef | ||
import fs2.io.tcp.SocketGroup | ||
import scodec.Codec | ||
import spinoco.protocol.http.codec.{HttpRequestHeaderCodec, HttpResponseHeaderCodec} | ||
import spinoco.protocol.http.{HttpRequestHeader, HttpResponseHeader, HttpStatusCode} | ||
|
@@ -36,7 +36,7 @@ object HttpServer { | |
* Request is not suplied if failure happened before request was constructed. | ||
* | ||
*/ | ||
def apply[F[_] : ConcurrentEffect : Timer]( | ||
def apply[F[_] : ConcurrentEffect : Timer: ContextShift]( | ||
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. Could we (and same for client) get rid of apply[F] as being constructor? Lets do mk, and have apply as summoner of the instance 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. Changed them both to mk, however server cannot have summoner instance as the result is Stream[F, Unit]. |
||
maxConcurrent: Int = Int.MaxValue | ||
, receiveBufferSize: Int = 256 * 1024 | ||
, maxHeaderSize: Int = 10 *1024 | ||
|
@@ -48,19 +48,18 @@ object HttpServer { | |
, requestFailure : Throwable => Stream[F, HttpResponse[F]] | ||
, sendFailure: (Option[HttpRequestHeader], HttpResponse[F], Throwable) => Stream[F, Nothing] | ||
)( | ||
implicit | ||
AG: AsynchronousChannelGroup | ||
socketGroup: SocketGroup | ||
): Stream[F, Unit] = { | ||
import Stream._ | ||
import internal._ | ||
import spinoco.fs2.http.internal._ | ||
val (initial, readDuration) = requestHeaderReceiveTimeout match { | ||
case fin: FiniteDuration => (true, fin) | ||
case _ => (false, 0.millis) | ||
} | ||
|
||
io.tcp.server[F](bindTo, receiveBufferSize = receiveBufferSize).map { resource => | ||
socketGroup.server[F](bindTo, receiveBufferSize = receiveBufferSize).map { resource => | ||
Stream.resource(resource).flatMap { socket => | ||
eval(SignallingRef(initial)).flatMap { timeoutSignal => | ||
eval(SignallingRef[F, Boolean](initial)).flatMap { timeoutSignal => | ||
readWithTimeout[F](socket, readDuration, timeoutSignal.get, receiveBufferSize) | ||
.through(HttpRequest.fromStream(maxHeaderSize, requestCodec)) | ||
.flatMap { case (request, body) => | ||
|
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.
Do we need still the timer?
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.
Yes, it is required for the websocket client, as we need to do pings there.