Skip to content

Instantly share code, notes, and snippets.

@ast
Last active May 7, 2018 18:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ast/a41816345e94e065890440e87e41a219 to your computer and use it in GitHub Desktop.
Save ast/a41816345e94e065890440e87e41a219 to your computer and use it in GitHub Desktop.
Minimal viable epoll package for go
package epoll
import (
"golang.org/x/sys/unix"
"os"
)
type Op uint32
const (
// Just a subset, check the complete x/sys/unix documentation
EpollIn Op = unix.EPOLLIN
EpollOut = unix.EPOLLOUT
EpollPri = unix.EPOLLPRI
EpollErr = unix.EPOLLERR
// It will probably not do what you have in mind without this flag.
// Without you will get multiple events while the fd is ready.
EpollEt = unix.EPOLLET
)
type Event struct {
File *os.File
Ops Op
}
type EpollWatcher struct {
Events chan Event
Errors chan error
epfd int
files map[int]*os.File
}
/* internal waiting function */
func (w *EpollWatcher) epollWait() {
events := make([]unix.EpollEvent, 10)
timeout_msec := -1
for {
n, err := unix.EpollWait(w.epfd, events, timeout_msec)
if err != nil {
w.Errors <- err
return
}
for _, e := range events[:n] {
w.Events <- Event{File: w.files[int(e.Fd)], Ops: Op(e.Events)}
}
}
}
func NewEpollWatcher() (*EpollWatcher, error) {
epfd, err := unix.EpollCreate(1) // argument ignored since Linux 2.6.8
if err != nil {
return nil, err
}
w := &EpollWatcher{Events: make(chan Event), Errors: make(chan error), epfd: epfd}
w.files = make(map[int]*os.File)
go w.epollWait() // enter waiting loop
return w, nil
}
func (w *EpollWatcher) Close() error {
return unix.Close(w.epfd)
}
func (w *EpollWatcher) Add(file *os.File, ops Op) error {
events := unix.EpollEvent{
Events: uint32(ops),
Fd: int32(file.Fd()),
}
err := unix.EpollCtl(w.epfd, unix.EPOLL_CTL_ADD, int(file.Fd()), &events)
if err != nil {
return err
}
// keep a map of fd to file so we can return the file object
w.files[int(file.Fd())] = file
return nil
}
func (w *EpollWatcher) Modify(file *os.File, ops Op) error {
events := unix.EpollEvent{
Events: uint32(ops),
Fd: int32(file.Fd()),
}
return unix.EpollCtl(w.epfd, unix.EPOLL_CTL_MOD, int(file.Fd()), &events)
}
func (w *EpollWatcher) Remove(file os.File) error {
err := unix.EpollCtl(w.epfd, unix.EPOLL_CTL_DEL, int(file.Fd()), nil)
if err != nil {
return err
}
delete(w.files, int(file.Fd()))
return nil
}
@purpleidea
Copy link

purpleidea commented May 7, 2018

Thanks for the example! A small API comment. In:

type EpollWatcher struct {
	Events chan Event
	Errors chan error
	...
}

I'd recommend instead a single channel which returns a struct like:

type Event struct {
	File *os.File
	Ops Op
	Err error // added this here instead of separate error channel
}

With this approach, you either send an Event which has just the error set, or the File/Ops/Etc set. That way a receiver can ensure the messages are linearizable if there is ever more than one writer to the channel in the future. Similarly, you only need to watch for a single channel close signal instead of two. That close should probably be added somewhere.

HTH and thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment