effekt icon indicating copy to clipboard operation
effekt copied to clipboard

Concurrent IO

Open b-studios opened this issue 11 months ago • 4 comments

This eventually replaces #324.

There is still a lot to do, but some mechanisms are already in place.

b-studios avatar Mar 22 '24 18:03 b-studios

This is how you can use the various libraries:

import io
import io/network
import io/time
import immutable/bytes
import text/string

// creates a simple echo server.
def main() = eventloop(box {
  var x in global = 0;
  server("localhost", 8080, box {
    loop {
      val msg = do receive();
      x = x + 1
      //println("Received a request: " ++ msg.toString)
      if (msg.toString.startsWith("GET /")) {
        if (x <= 1) {
          wait(1000)
        }
        do send(toBytes("HTTP/1.1 200 OK\r\n\r\nHello from Effekt!"))
      } else {
        do send("HTTP/1.1 400 Bad Request\r\n\r\n".toBytes)
      }
      do end()
    }
  })
})

However, it will currently crash, since global is not bound and the temporary hack only worked for a single file (and not across several other files).

b-studios avatar Mar 25 '24 16:03 b-studios

@jiribenes With #434 being merged, is the library now usable for you?

b-studios avatar Mar 27 '24 17:03 b-studios

@jiribenes With #434 being merged, is the library now usable for you?

Yep -- except that immutable/bytes is not a part of this PR so nobody else can actually use this. It would be nice to have a test or two to detect such problems if at all possible.

Here's what I tried with my `http` library
import io
import io/network
import immutable/bytes

import http

def serve(): Unit / Socket = {
  val _ = do receive();
  
  val responseBody = http::Body("Hello from Effekt!".toBytes)
  do send(http::ok(responseBody).encode)
  do end()
}

def main() = 
  eventloop(box {
    server("localhost", 8080, box {
      serve()
    })
  })

I can also confirm that do end() is not necessary if you populate the Content-Length header :)

jiribenes avatar Mar 28 '24 10:03 jiribenes

Agreed, there should be tests. I simply forgot to add the file :(

b-studios avatar Mar 28 '24 10:03 b-studios

Superseded by #469

b-studios avatar May 24 '24 16:05 b-studios