La selezione su un canale e un descrittore di file non è possibile perché le astrazioni si trovano a livelli diversi. I canali sono gestiti dal runtime go e dai descrittori di file dal sistema operativo. Quello di cui hai bisogno è creare un ponte tra loro e questo può essere fatto con un net.Pipe()
.
Praticamente ciò che devi fare è dedicare una goroutine a epoll()
/select()
per guardare i vostri zmq-prese e un unico "sveglia" net.Pipe()
. Questo è il tuo server di polling. Un'altra goroutine ascolta i tuoi canali di lettura e scrittura. Quando qualcuno invia i canali di lettura o scrittura, la seconda goroutine invia sulla pipe per riattivare il server di polling.
Ecco come funziona il pacchetto di rete nella libreria standard. Consiglio vivamente di leggerlo per ispirazione (o rubare ... la licenza BSD è molto liberale).
Questa è una descrizione di pollServer dalla rete stessa. Potrebbe essere necessario leggere il codice per capire cosa sta dicendo, ma questa sezione da fd.go
dovrebbe essere un buon punto di partenza per la ricerca.
// A pollServer helps FDs determine when to retry a non-blocking
// read or write after they get EAGAIN. When an FD needs to wait,
// send the fd on s.cr (for a read) or s.cw (for a write) to pass the
// request to the poll server. Then receive on fd.cr/fd.cw.
// When the pollServer finds that i/o on FD should be possible
// again, it will send fd on fd.cr/fd.cw to wake any waiting processes.
// This protocol is implemented as s.WaitRead() and s.WaitWrite().
//
// There is one subtlety: when sending on s.cr/s.cw, the
// poll server is probably in a system call, waiting for an fd
// to become ready. It's not looking at the request channels.
// To resolve this, the poll server waits not just on the FDs it has
// been given but also its own pipe. After sending on the
// buffered channel s.cr/s.cw, WaitRead/WaitWrite writes a
// byte to the pipe, causing the pollServer's poll system call to
// return. In response to the pipe being readable, the pollServer
// re-polls its request channels.
//
// Note that the ordering is "send request" and then "wake up server".
// If the operations were reversed, there would be a race: the poll
// server might wake up and look at the request channel, see that it
// was empty, and go back to sleep, all before the requester managed
// to send the request. Because the send must complete before the wakeup,
// the request channel must be buffered. A buffer of size 1 is sufficient
// for any request load. If many processes are trying to submit requests,
// one will succeed, the pollServer will read the request, and then the
// channel will be empty for the next process's request. A larger buffer
// might help batch requests.
//
// To avoid races in closing, all fd operations are locked and
// refcounted. when netFD.Close() is called, it calls syscall.Shutdown
// and sets a closing flag. Only when the last reference is removed
// will the fd be closed.
Buona fortuna re-implementazione rete. La buona notizia alla fine di tutto questo il tuo zmq-socket sarà thread-safe in viaggio.
fonte
2012-07-05 14:21:43
Solo per curiosità, questo è il genere di cose che puoi fare in altri progetti come https://github.com/duckie/boson –