Open lesismal opened 1 year ago
OnRead
does. What does "handle the reading event" mean? Note that at least with our current approaches, we can't in general know whether there is any data available to read without attempting to read something.I don't understand what OnRead does. What does "handle the reading event" mean? Note that at least with our current approaches, we can't in general know whether there is any data available to read without attempting to read something.
OnRead registers a callback, users no longer use for { read }
in a goroutine. but when the fd is readable, runtime calls the callback and users can read data in the callback.
It's like Reactor
, io events passed to the users' handler.
I would want to see an implementation before accepting a proposal like this.
It's also like the OnReadable
in https://github.com/golang/go/issues/15735#issuecomment-266574151
I have implemented similar features in my lib, an example:
package main
import (
"fmt"
"github.com/lesismal/nbio"
)
func main() {
engine := nbio.NewEngine(nbio.Config{
Network: "tcp",
Addrs: []string{":8080"},
})
engine.OnData(func(c *nbio.Conn, data []byte) {
c.Write(append([]byte{}, data...))
})
err := engine.Start()
if err != nil {
fmt.Printf("nbio.Start failed: %v\n", err)
return
}
defer engine.Stop()
<-make(chan int)
}
I would want to see an implementation before accepting a proposal like this.
I haven't tried to implement these Reactor
things in runtime yet, but found that maybe can do it for different Socket Conns when I read go source code.
I'll try it in my spare time.
Related: https://github.com/golang/go/issues/15735
I think it will be more useful to provide these non-blocking methods by RawConn rather than TCPConn.
These new metheds and the new definition would be like(see the OnRead/OnWrite/EnablePollWrite:
After providing these new methods, users will be able to choose how to handle Fd's IO:
The old issue https://github.com/golang/go/issues/15735 was only for TCPConn, and still no solution to solve the problem of saving goroutines and blocking methods, so I open this new issue.