question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creation of session should return `F[Session[F]]`

See original GitHub issue

Creation 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:closed
  • Created 4 years ago
  • Comments:21 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
BalmungSancommented, Jul 4, 2019

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:

trait Resource[F[_]] {
  type R[_, _] // I think I have a syntax error here, since the first parameter should be like F[_].

  def create[T](t: => T): R[F, T]
}

Where for IO R[_, _] == Resource[IO, T] and for Future R[_, _] = Id[Id, T] Thus Future users could do this:

val driver: Driver[Future] = GraphDatabase[Future].driver(...)
val session: Session[Future] = driver.session(...)

While IO (and friend) users will have:

val session: Resource[IO, Session[IO]] = for {
  driver <- GraphDatabase[IO].driver(...)
  session <- driver.session(...)
} yield session

(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!

2reactions
gvolpecommented, Jun 29, 2019

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 in Main but it could also be accessed from anywhere in the program by doing Main.s (yes, you could restrict this by adding private 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 call mkSession 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 by flatMap and pass it as an argument.

Again, I recommend watching Fabio’s talk, he does a great job explaining this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found