zio / zio-nio

A small, unopinionated ZIO interface to NIO.
https://zio.dev/zio-nio
Apache License 2.0
188 stars 76 forks source link

channel.read has side effect without type representation #206

Open ubourdon opened 4 years ago

ubourdon commented 4 years ago

I talk from zio.nio part of the lib (not core) If i understand correclty, channel.read

def f(channel: AsynchronousSocketChannel) = {
   val _first3: IO[Exception, Chunk[Byte]] = channel.read(3)
   val _second3: IO[Exception, Chunk[Byte]] = channel.read(3)
}

read 3 first byte of the buffer link to the channel, and if we redo channel.read(3) that read the next 3 byte of the buffer for this channel right ?

From a FP/ZIO point of view is it not a huge issue ?

How can I simply perform for example, an recursive/fold read of the channel with chunk of byte ? What i mean, i have channel with X bytes, i decide to use 80 byte buffer, to be sure i read all, i need to iterate on my channel until it say me "ok you read all" (equivalent to buffer return -1).

tusharmath commented 4 years ago

@ubourdon I don't think there is any concept of "read all" from a Socket's point of view. Socket's keep sending and receiving messages until they are closed. Buffer's, on the other hand, have a finite capacity and so have a concept of "final" and "end of file".

Typically if you are implementing an HTTP server, you "reading" any message that comes in, until a \r\n is received, which will mark the end of an HTTP request.

ubourdon commented 4 years ago

To do that, you must specify a buffer size and read this size several time until you find \r\n no ?

ubourdon commented 4 years ago

But beyond that,

channel.read mutate the state of channel without any type returned by the action.

channel.read don't return same result if i call it 2 or moe times, that brake the Referential transparency principle.