Creation of session should return `F[Session[F]]`
See original GitHub issueCreation of mutable state should be wrapped in F
for the reasons stated here and here. Fabio Labella also gave a great talk on the topic.
val session: Session[IO] = driver.session().asScala[IO] // this can leak
val makeSession: IO[Session[IO]] = driver.session.asScala[IO] // this is the action of creating a session
makeSession.flatMap { s => // here we create a region of sharinn
useSession(s) // whoever needs access to the session should take it as a parameter
// in this way the sharing of the session is under our control in a narrow scope (inside `flatMap`)
}
Right now I get around by just wrapping its creation in IO
but ideally the library should do this.
This probably doesn’t make sense when using Future
since it breaks referential transparency but for the rest of us using FP libraries this is important.
Let me know if I can be of any help.
Issue Analytics
- State:
- Created 4 years ago
- Comments:21 (13 by maintainers)
Top Results From Across the Web
How to Use Sessions (Sun Java System Web Server 6.1 SP6 ...
Returns a Boolean value indicating if the session is new. It's a new session if the server has created it and the client...
Read more >Hibernate - Sessions - Tutorialspoint
The session objects should not be kept open for a long time because they are not usually thread safe and they should be...
Read more >How to Use Sessions and Session Variables in PHP - Code
Session handling is a key concept in PHP that enables user information to be persisted across all the pages of a website or...
Read more >session - Amazon Web Services - Go SDK
NewSession returns a new Session created from SDK defaults, config files, environment, and user provided config files. Once the Session is created it...
Read more >How to use session in Go · Build web application with Golang
SessionRead returns a session represented by the corresponding sid. Creates a new session and returns it if it does not already exist. SessionDestroy...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think some kind of
Resource
typeclass from our side may work. Since it seems the only area of friction is really just the Driver / Session creation and lifecycle management. For the rest of the code, it seems both worlds can coexist without problems.Maybe something like:
Where for IO
R[_, _] == Resource[IO, T]
and for FutureR[_, _] = Id[Id, T]
ThusFuture
users could do this:While IO (and friend) users will have:
(For Zio folks it will be a
ZManaged
instead).This is just an sketch I just made on top of my head, I am not totally sure if both cases can be supported as seamlessly as we want. But I do think it is worth exploring!
No worries. The concept of mutable state sharing is so simple that is hard to get at first 😃
In the first example (the one that leaks a session) we could potentially have unintended access to our session. This compares pretty much to global state. The variable
s
can be accessed anywhere inMain
but it could also be accessed from anywhere in the program by doingMain.s
(yes, you could restrict this by addingprivate
but the first problem remains).In the second (and safe) example we are making explicit when some mutable state is being created as we don’t want to unintentionally share it. So it is wrapped in
IO
(or any other arbitrary effect). If we callmkSession
is obvious that we are creating a new session, you only need to look at its type signature. And if we want to share this session then you need to create an explicit region of sharing which is dictated byflatMap
and pass it as an argument.Again, I recommend watching Fabio’s talk, he does a great job explaining this.