ipolyzos
11/30/2022, 7:42 AMResource
or a Bracket
2. Using bracket when the url is invalid for example
bracketCase(
acquire = { openConnection("<https://someURL>") },
use = { connection ->
connection.fetch()
},
release = { connection, exitCase ->
when(exitCase) {
is ExitCase.Completed -> println("Completed: $exitCase")
is ExitCase.Cancelled -> println("Cancelled: $exitCase")
is ExitCase.Failure ->
println("Failure: $exitCase")
}
println("Closing connection")
closeConnection(connection)
}
)
i do get the exit case but i see the exception is still thrown.. What's the best way to handle the exception properly? do i need to wrap the computation with an either
block?
Failure: Failure(failure=java.net.UnknownHostException: someURL)
Closing connection
Exception in thread "main" java.net.UnknownHostException: someURL
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
at java.base/java.net.Socket.connect(Socket.java:633)
at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:304)
at java.base/sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:174)
finally since its a SSE connection do u think there are other things that need to be accounted for other than running the processing of the events in the Dispatchers.IO pool?simon.vergauwen
11/30/2022, 7:45 AMresource
, it's easier to understand with a simpler API and re-useable when you need it to be. There is no performance penalty for using Resource
.
This snippet with the current Alpha and 2.x.x is.
resourceScope {
val connection = install({ openConnection("<https://someURL>") }) { conn, exitCase ->
log(exitCase)
closeConnection(conn)
}
connection.fetch()
}
If you won't wan this to throw, you need to wrap it in Either.catch
. Alternatively you can have connection.fetch()
return Either
and do.
either {
resourceScope {
val conn = connection().bind()
conn.fetch().bind()
}
}
simon.vergauwen
11/30/2022, 7:46 AMsimon.vergauwen
11/30/2022, 7:48 AM<http://Dispatchers.IO|Dispatchers.IO>
should be enough. Depending on how and where you run it using SuspendApp
might be interesting. https://github.com/arrow-kt/suspendappipolyzos
11/30/2022, 7:50 AMsimon.vergauwen
11/30/2022, 7:51 AMResource
composes pretty nicely with everything and you can use it's DSL if you create extension function on it.
suspend fun ResourceScope.connection(url: String): Connection =
install({ openConnection("<https://someURL>") }) { conn, exitCase ->
log(exitCase, url)
conn.close()
}
simon.vergauwen
11/30/2022, 7:53 AMipolyzos
11/30/2022, 7:55 AMsimon.vergauwen
11/30/2022, 7:56 AMFlow
tbh so that you can just rely on the automatic back-pressure to keep them open.
We have Resource#asFlow
as well to do this 😉simon.vergauwen
11/30/2022, 7:57 AMipolyzos
11/30/2022, 7:58 AMsimon.vergauwen
11/30/2022, 9:07 AM