This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Migration to ZIO 2.0 - run the scalafix and do obvious manual fixes * Migrate transducers to ZIO 2.0 * Chip away more errors * Finish code migration to ZIO 2.0, migrate tests * Final tweaks * Add disableAutoTrace all over the place * Bump zio snapshot, remove TODO * Bump zio, improve example, add tracing to ManagedOps#useForked * initial work * fix version specific issue * format * update documentation * upgrade scala 3 version * blocking * format * fix conflicting versions Co-authored-by: Zdeněk Hřebíček <10925068+zhrebicek@users.noreply.github.com> Co-authored-by: Zdeněk Hřebíček <zdenek.hrebicek@gmail.com>
- Loading branch information
1 parent
a5452df
commit 84aa783
Showing
66 changed files
with
1,537 additions
and
1,183 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,37 @@ | ||
project/zecret | ||
project/travis-deploy-key | ||
project/secrets.tar.xz | ||
target | ||
test-output/ | ||
.sbtopts | ||
.bsp | ||
project/.sbt | ||
*.tmp | ||
website/i18n | ||
website/yarn.lock | ||
website/static/api | ||
test-output/ | ||
.bloop | ||
.metals | ||
metals.sbt | ||
*/metals.sbt | ||
.idea | ||
coursier | ||
.DS_Store | ||
metals.sbt | ||
project/metals.sbt | ||
project/project/metals.sbt | ||
sbt.json | ||
.bsp/ | ||
project/project/ | ||
*.iml | ||
|
||
# if you are here to add your IDE's files please read this instead: | ||
# https://stackoverflow.com/questions/7335420/global-git-ignore#22885996 | ||
website/node_modules | ||
website/.docusaurus | ||
website/build | ||
website/docs | ||
website/static/api* | ||
website/versioned_docs | ||
website/i18n/en.json | ||
website/yarn.lock | ||
website/package-lock.json | ||
website/static/api | ||
.bsp/ |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"files.watcherExclude": { | ||
"**/target": true | ||
} | ||
} |
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
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,55 +1,52 @@ | ||
package zio.nio.examples | ||
|
||
import zio._ | ||
import zio.clock.Clock | ||
import zio.duration._ | ||
import zio.nio.InetSocketAddress | ||
import zio.nio.channels.AsynchronousServerSocketChannel | ||
import zio.stream._ | ||
import zio.{Clock, Console, ExitCode, Managed, RIO, URIO, ZIO, ZIOAppDefault, ZTraceElement, durationInt} | ||
|
||
object StreamsBasedServer extends App { | ||
object StreamsBasedServer extends ZIOAppDefault { | ||
|
||
def run(args: List[String]): URIO[zio.console.Console with Clock with zio.console.Console, ExitCode] = | ||
def run: URIO[Console with Clock with Console, ExitCode] = | ||
ZStream | ||
.managed(server(8080)) | ||
.flatMap(handleConnections(_) { chunk => | ||
console.putStrLn(s"Read data: ${chunk.mkString}") *> | ||
clock.sleep(2.seconds) *> | ||
console.putStrLn("Done").ignore | ||
Console.printLine(s"Read data: ${chunk.mkString}") *> | ||
Clock.sleep(2.seconds) *> | ||
Console.printLine("Done").ignore | ||
}) | ||
.runDrain | ||
.orDie | ||
.exitCode | ||
|
||
def server(port: Int): Managed[Exception, AsynchronousServerSocketChannel] = | ||
def server(port: Int)(implicit trace: ZTraceElement): Managed[Exception, AsynchronousServerSocketChannel] = | ||
for { | ||
server <- AsynchronousServerSocketChannel.open | ||
socketAddress <- InetSocketAddress.wildCard(port).toManaged_ | ||
_ <- server.bindTo(socketAddress).toManaged_ | ||
socketAddress <- InetSocketAddress.wildCard(port).toManaged | ||
_ <- server.bindTo(socketAddress).toManaged | ||
} yield server | ||
|
||
def handleConnections[R <: console.Console]( | ||
def handleConnections[R <: Console]( | ||
server: AsynchronousServerSocketChannel | ||
)(f: String => RIO[R, Unit]): ZStream[R, Throwable, Unit] = | ||
)(f: String => RIO[R, Unit])(implicit trace: ZTraceElement): ZStream[R, Throwable, Unit] = | ||
ZStream | ||
.repeatEffect(server.accept.preallocate) | ||
.map(conn => ZStream.managed(conn.ensuring(console.putStrLn("Connection closed").ignore).withEarlyRelease)) | ||
.repeatZIO(server.accept.preallocate) | ||
.map(conn => ZStream.managed(conn.ensuring(Console.printLine("Connection closed").ignore).withEarlyRelease)) | ||
.flatMapPar[R, Throwable, Unit](16) { connection => | ||
connection.mapM { case (closeConn, channel) => | ||
connection.mapZIO { case (closeConn, channel) => | ||
for { | ||
_ <- console.putStrLn("Received connection") | ||
_ <- Console.printLine("Received connection") | ||
data <- ZStream | ||
.fromEffectOption( | ||
channel.readChunk(64).tap(_ => console.putStrLn("Read chunk")).orElse(ZIO.fail(None)) | ||
.fromZIOOption( | ||
channel.readChunk(64).tap(_ => Console.printLine("Read chunk")).orElse(ZIO.fail(None)) | ||
) | ||
.flattenChunks | ||
.take(4) | ||
.transduce(ZTransducer.utf8Decode) | ||
.via(ZPipeline.utf8Decode) | ||
.run(Sink.foldLeft("")(_ + (_: String))) | ||
_ <- closeConn | ||
_ <- f(data) | ||
} yield () | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.