evio/evio_loop.go

621 lines
13 KiB
Go
Raw Normal View History

2017-11-02 18:08:18 +03:00
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2017-10-28 22:23:13 +03:00
// +build netbsd openbsd freebsd darwin dragonfly linux
2017-10-26 23:43:31 +03:00
2017-10-29 00:58:59 +03:00
package evio
2017-10-26 23:43:31 +03:00
import (
"net"
"os"
"sort"
"strconv"
"sync"
"syscall"
"time"
2017-10-28 22:23:13 +03:00
2017-10-29 00:58:59 +03:00
"github.com/tidwall/evio/internal"
2017-10-26 23:43:31 +03:00
)
func (ln *listener) close() {
if ln.fd != 0 {
syscall.Close(ln.fd)
}
if ln.f != nil {
ln.f.Close()
}
if ln.ln != nil {
ln.ln.Close()
}
if ln.network == "unix" {
os.RemoveAll(ln.addr)
}
}
2017-11-07 19:52:16 +03:00
// system takes the net listener and detaches it from it's parent
// event loop, grabs the file descriptor, and makes it non-blocking.
2017-10-26 23:43:31 +03:00
func (ln *listener) system() error {
var err error
switch netln := ln.ln.(type) {
default:
2017-11-07 19:52:16 +03:00
panic("invalid listener type")
2017-10-26 23:43:31 +03:00
case *net.TCPListener:
ln.f, err = netln.File()
case *net.UnixListener:
ln.f, err = netln.File()
}
if err != nil {
ln.close()
return err
}
ln.fd = int(ln.f.Fd())
return syscall.SetNonblock(ln.fd, true)
}
2017-11-07 19:52:16 +03:00
// unixConn represents the connection as the event loop sees it.
// This is also becomes a detached connection.
2017-10-31 00:02:10 +03:00
type unixConn struct {
2017-11-07 16:49:33 +03:00
id, fd int
outbuf []byte
outpos int
action Action
opts Options
timeout time.Time
err error
2017-11-07 19:52:16 +03:00
dialerr error
2017-11-07 16:49:33 +03:00
wake bool
writeon bool
detached bool
closed bool
opening bool
}
func (c *unixConn) Timeout() time.Time {
return c.timeout
2017-10-31 00:02:10 +03:00
}
func (c *unixConn) Read(p []byte) (n int, err error) {
2017-11-01 03:44:57 +03:00
return syscall.Read(c.fd, p)
2017-10-31 00:02:10 +03:00
}
func (c *unixConn) Write(p []byte) (n int, err error) {
if c.detached {
2017-11-01 03:44:57 +03:00
if len(c.outbuf) > 0 {
for len(c.outbuf) > 0 {
n, err = syscall.Write(c.fd, c.outbuf)
if n > 0 {
c.outbuf = c.outbuf[n:]
}
if err != nil {
return 0, err
}
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
c.outbuf = nil
}
var tn int
if len(p) > 0 {
for len(p) > 0 {
n, err = syscall.Write(c.fd, p)
if n > 0 {
p = p[n:]
tn += n
}
if err != nil {
return tn, err
}
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
p = nil
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
return tn, nil
2017-10-31 00:02:10 +03:00
}
2017-11-01 03:44:57 +03:00
return syscall.Write(c.fd, p)
2017-10-31 00:02:10 +03:00
}
func (c *unixConn) Close() error {
2017-11-02 15:03:54 +03:00
if c.closed {
return syscall.EINVAL
}
2017-10-31 00:02:10 +03:00
err := syscall.Close(c.fd)
2017-11-02 15:03:54 +03:00
c.fd = -1
c.closed = true
2017-10-31 00:02:10 +03:00
return err
2017-10-26 23:43:31 +03:00
}
func serve(events Events, lns []*listener) error {
2017-10-28 22:23:13 +03:00
p, err := internal.MakePoll()
2017-10-26 23:43:31 +03:00
if err != nil {
return err
}
defer syscall.Close(p)
for _, ln := range lns {
2017-10-28 22:23:13 +03:00
if err := internal.AddRead(p, ln.fd); err != nil {
2017-10-26 23:43:31 +03:00
return err
}
}
var mu sync.Mutex
2017-11-07 19:52:16 +03:00
var done bool
2017-10-26 23:43:31 +03:00
lock := func() { mu.Lock() }
unlock := func() { mu.Unlock() }
2017-10-31 00:02:10 +03:00
fdconn := make(map[int]*unixConn)
idconn := make(map[int]*unixConn)
2017-11-07 16:49:33 +03:00
timeoutqueue := internal.NewTimeoutQueue()
2017-11-06 13:58:06 +03:00
var id int
2017-11-07 19:52:16 +03:00
dial := func(addr string, timeout time.Duration) (int, bool) {
lock()
if done {
2017-11-06 13:58:06 +03:00
unlock()
2017-11-07 19:52:16 +03:00
return 0, false
}
id++
c := &unixConn{id: id, opening: true}
idconn[id] = c
if timeout != 0 {
c.timeout = time.Now().Add(timeout)
timeoutqueue.Push(c)
}
unlock()
// resolving an address blocks and we don't want blocking, like ever.
// but since we're leaving the event loop we'll need to complete the
// socket connection in a goroutine and add the read and write events
// to the loop to get back into the loop.
go func() {
err := func() error {
sa, err := resolve(addr)
2017-11-06 13:58:06 +03:00
if err != nil {
2017-11-07 19:52:16 +03:00
return err
2017-11-06 13:58:06 +03:00
}
2017-11-07 19:52:16 +03:00
var fd int
switch sa.(type) {
case *syscall.SockaddrUnix:
fd, err = syscall.Socket(syscall.AF_UNIX, syscall.SOCK_STREAM, 0)
case *syscall.SockaddrInet4:
2017-11-07 16:49:33 +03:00
fd, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
2017-11-07 19:52:16 +03:00
case *syscall.SockaddrInet6:
2017-11-07 16:49:33 +03:00
fd, err = syscall.Socket(syscall.AF_INET6, syscall.SOCK_STREAM, 0)
2017-11-06 13:58:06 +03:00
}
2017-11-07 19:52:16 +03:00
err = syscall.Connect(fd, sa)
if err != nil && err != syscall.EINPROGRESS {
syscall.Close(fd)
return err
}
if err := syscall.SetNonblock(fd, true); err != nil {
syscall.Close(fd)
return err
}
lock()
err = internal.AddRead(p, fd)
if err != nil {
unlock()
syscall.Close(fd)
return err
}
err = internal.AddWrite(p, fd, &c.writeon)
if err != nil {
unlock()
syscall.Close(fd)
return err
}
c.fd = fd
fdconn[fd] = c
2017-11-06 13:58:06 +03:00
unlock()
2017-11-07 19:52:16 +03:00
return nil
}()
2017-11-06 13:58:06 +03:00
if err != nil {
2017-11-07 19:52:16 +03:00
// set a dial error and timeout right away
lock()
c.dialerr = err
c.timeout = time.Now()
2017-11-07 16:49:33 +03:00
timeoutqueue.Push(c)
2017-11-07 19:52:16 +03:00
unlock()
2017-11-06 13:58:06 +03:00
}
2017-11-07 19:52:16 +03:00
}()
return id, true
}
// wake wakes up a connection
wake := func(id int) bool {
var ok = true
var err error
lock()
if done {
2017-11-06 13:58:06 +03:00
unlock()
2017-11-07 19:52:16 +03:00
return false
}
c := idconn[id]
if c == nil || c.fd == 0 {
ok = false
} else if !c.wake {
c.wake = true
err = internal.AddWrite(p, c.fd, &c.writeon)
}
unlock()
if err != nil {
panic(err)
}
return ok
2017-11-06 13:58:06 +03:00
}
2017-11-07 19:52:16 +03:00
ctx := Server{Wake: wake, Dial: dial}
2017-11-06 13:58:06 +03:00
ctx.Addrs = make([]net.Addr, len(lns))
for i, ln := range lns {
ctx.Addrs[i] = ln.naddr
2017-10-26 23:43:31 +03:00
}
if events.Serving != nil {
2017-11-06 13:58:06 +03:00
switch events.Serving(ctx) {
2017-10-26 23:43:31 +03:00
case Shutdown:
return nil
}
}
defer func() {
lock()
2017-11-07 19:52:16 +03:00
done = true
2017-11-01 03:44:57 +03:00
type fdid struct {
2017-11-07 16:49:33 +03:00
fd, id int
opening bool
2017-11-01 03:44:57 +03:00
}
2017-10-26 23:43:31 +03:00
var fdids []fdid
2017-11-07 19:52:16 +03:00
for _, c := range idconn {
fdids = append(fdids, fdid{c.fd, c.id, c.opening})
2017-10-26 23:43:31 +03:00
}
sort.Slice(fdids, func(i, j int) bool {
return fdids[j].id < fdids[i].id
})
for _, fdid := range fdids {
2017-11-07 19:52:16 +03:00
if fdid.fd != 0 {
syscall.Close(fdid.fd)
}
2017-11-07 16:49:33 +03:00
if fdid.opening {
if events.Opened != nil {
laddr := getlocaladdr(fdid.fd)
raddr := getremoteaddr(fdid.fd)
unlock()
2017-11-07 19:52:16 +03:00
events.Opened(fdid.id, Info{
2017-11-07 16:49:33 +03:00
Closing: true,
AddrIndex: -1,
LocalAddr: laddr,
RemoteAddr: raddr,
})
lock()
}
}
2017-10-26 23:43:31 +03:00
if events.Closed != nil {
unlock()
2017-11-01 03:44:57 +03:00
events.Closed(fdid.id, nil)
2017-10-26 23:43:31 +03:00
lock()
}
}
2017-11-07 16:49:33 +03:00
syscall.Close(p)
2017-11-07 19:52:16 +03:00
fdconn = nil
idconn = nil
2017-10-26 23:43:31 +03:00
unlock()
}()
var packet [0xFFFF]byte
2017-10-28 22:23:13 +03:00
var evs = internal.MakeEvents(64)
2017-11-07 16:49:33 +03:00
nextTicker := time.Now()
2017-10-26 23:43:31 +03:00
for {
2017-11-07 16:49:33 +03:00
delay := nextTicker.Sub(time.Now())
if delay < 0 {
delay = 0
} else if delay > time.Second/4 {
delay = time.Second / 4
}
pn, err := internal.Wait(p, evs, delay)
2017-10-26 23:43:31 +03:00
if err != nil && err != syscall.EINTR {
return err
}
2017-11-07 19:52:16 +03:00
remain := nextTicker.Sub(time.Now())
if remain < 0 {
var tickerDelay time.Duration
var action Action
if events.Tick != nil {
tickerDelay, action = events.Tick()
if action == Shutdown {
return nil
2017-11-07 16:49:33 +03:00
}
2017-11-07 19:52:16 +03:00
} else {
tickerDelay = time.Hour
2017-11-07 16:49:33 +03:00
}
2017-11-07 19:52:16 +03:00
nextTicker = time.Now().Add(tickerDelay + remain)
2017-11-07 16:49:33 +03:00
}
2017-11-07 19:52:16 +03:00
// check for dial connection timeouts
2017-11-07 16:49:33 +03:00
if timeoutqueue.Len() > 0 {
var count int
now := time.Now()
for {
v := timeoutqueue.Peek()
if v == nil {
break
}
c := v.(*unixConn)
if now.After(v.Timeout()) {
timeoutqueue.Pop()
2017-11-07 19:52:16 +03:00
if _, ok := idconn[c.id]; ok && c.opening {
2017-11-07 16:49:33 +03:00
delete(idconn, c.id)
delete(fdconn, c.fd)
syscall.Close(c.fd)
if events.Opened != nil {
laddr := getlocaladdr(c.fd)
raddr := getremoteaddr(c.fd)
2017-11-07 19:52:16 +03:00
events.Opened(c.id, Info{
2017-11-07 16:49:33 +03:00
Closing: true,
AddrIndex: -1,
LocalAddr: laddr,
RemoteAddr: raddr,
})
}
if events.Closed != nil {
2017-11-07 19:52:16 +03:00
if c.dialerr != nil {
events.Closed(c.id, c.dialerr)
} else {
events.Closed(c.id, syscall.ETIMEDOUT)
}
2017-11-07 16:49:33 +03:00
}
count++
}
} else {
break
2017-10-26 23:43:31 +03:00
}
2017-11-07 16:49:33 +03:00
}
if count > 0 {
// invalidate the current events and wait for more
continue
2017-10-26 23:43:31 +03:00
}
}
lock()
for i := 0; i < pn; i++ {
var in []byte
2017-10-31 00:02:10 +03:00
var c *unixConn
2017-10-26 23:43:31 +03:00
var nfd int
var n int
var out []byte
var ln *listener
2017-10-29 23:09:40 +03:00
var lnidx int
2017-10-28 22:23:13 +03:00
var fd = internal.GetFD(evs, i)
2017-10-29 23:09:40 +03:00
for lnidx, ln = range lns {
2017-10-26 23:43:31 +03:00
if fd == ln.fd {
goto accept
}
}
2017-11-07 16:49:33 +03:00
ln = nil
2017-10-26 23:43:31 +03:00
c = fdconn[fd]
if c == nil {
syscall.Close(fd)
goto next
}
2017-11-07 16:49:33 +03:00
if c.opening {
2017-11-06 13:58:06 +03:00
2017-11-07 16:49:33 +03:00
lnidx = -1
goto opened
2017-11-06 13:58:06 +03:00
}
2017-10-26 23:43:31 +03:00
goto read
accept:
2017-11-07 16:49:33 +03:00
nfd, _, err = syscall.Accept(fd)
2017-10-26 23:43:31 +03:00
if err != nil {
goto next
}
if err = syscall.SetNonblock(nfd, true); err != nil {
goto fail
}
2017-10-28 22:23:13 +03:00
if err = internal.AddRead(p, nfd); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
id++
2017-11-07 16:49:33 +03:00
c = &unixConn{id: id, fd: nfd}
2017-10-26 23:43:31 +03:00
fdconn[nfd] = c
idconn[id] = c
2017-11-07 16:49:33 +03:00
goto opened
opened:
2017-10-26 23:43:31 +03:00
if events.Opened != nil {
2017-11-07 16:49:33 +03:00
laddr := getlocaladdr(fd)
raddr := getremoteaddr(fd)
2017-10-26 23:43:31 +03:00
unlock()
2017-11-07 19:52:16 +03:00
out, c.opts, c.action = events.Opened(c.id, Info{
2017-11-07 16:49:33 +03:00
AddrIndex: lnidx,
LocalAddr: laddr,
RemoteAddr: raddr,
})
2017-10-26 23:43:31 +03:00
lock()
if c.opts.TCPKeepAlive > 0 {
2017-11-07 16:49:33 +03:00
internal.SetKeepAlive(c.fd, int(c.opts.TCPKeepAlive/time.Second))
2017-10-26 23:43:31 +03:00
}
if len(out) > 0 {
c.outbuf = append(c.outbuf, out...)
}
}
2017-11-07 16:49:33 +03:00
if c.opening {
c.opening = false
goto next
}
2017-10-26 23:43:31 +03:00
goto write
read:
if c.action != None {
goto write
}
if c.wake {
c.wake = false
} else {
2017-10-31 00:02:10 +03:00
n, err = c.Read(packet[:])
2017-10-26 23:43:31 +03:00
if n == 0 || err != nil {
if err == syscall.EAGAIN {
goto write
}
2017-11-01 03:44:57 +03:00
c.err = err
2017-10-26 23:43:31 +03:00
goto close
}
in = append([]byte{}, packet[:n]...)
}
if events.Data != nil {
unlock()
out, c.action = events.Data(c.id, in)
lock()
}
if len(out) > 0 {
c.outbuf = append(c.outbuf, out...)
}
goto write
write:
if len(c.outbuf)-c.outpos > 0 {
if events.Prewrite != nil {
unlock()
action := events.Prewrite(c.id, len(c.outbuf[c.outpos:]))
lock()
2017-10-28 03:01:03 +03:00
if action == Shutdown {
2017-10-26 23:43:31 +03:00
c.action = Shutdown
}
}
2017-10-31 00:02:10 +03:00
n, err = c.Write(c.outbuf[c.outpos:])
2017-10-26 23:43:31 +03:00
if events.Postwrite != nil {
amount := n
if amount < 0 {
amount = 0
}
unlock()
action := events.Postwrite(c.id, amount, len(c.outbuf)-c.outpos-amount)
lock()
2017-10-28 03:01:03 +03:00
if action == Shutdown {
2017-10-26 23:43:31 +03:00
c.action = Shutdown
}
}
if n == 0 || err != nil {
if c.action == Shutdown {
goto close
}
if err == syscall.EAGAIN {
2017-11-07 16:49:33 +03:00
if err = internal.AddWrite(p, c.fd, &c.writeon); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
goto next
}
2017-11-01 03:44:57 +03:00
c.err = err
2017-10-26 23:43:31 +03:00
goto close
}
c.outpos += n
if len(c.outbuf)-c.outpos == 0 {
c.outpos = 0
2017-11-03 04:31:36 +03:00
c.outbuf = c.outbuf[:0]
2017-10-26 23:43:31 +03:00
}
}
if c.action == Shutdown {
goto close
}
if len(c.outbuf)-c.outpos == 0 {
if !c.wake {
2017-11-07 16:49:33 +03:00
if err = internal.DelWrite(p, c.fd, &c.writeon); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
}
if c.action != None {
goto close
}
} else {
2017-11-07 16:49:33 +03:00
if err = internal.AddWrite(p, c.fd, &c.writeon); err != nil {
2017-10-26 23:43:31 +03:00
goto fail
}
}
goto next
close:
delete(fdconn, c.fd)
delete(idconn, c.id)
2017-11-07 16:49:33 +03:00
//delete(idtimeout, c.id)
2017-10-26 23:43:31 +03:00
if c.action == Detach {
if events.Detached != nil {
2017-10-31 00:02:10 +03:00
c.detached = true
if len(c.outbuf)-c.outpos > 0 {
c.outbuf = append(c.outbuf[:0], c.outbuf[c.outpos:]...)
} else {
c.outbuf = nil
}
c.outpos = 0
syscall.SetNonblock(c.fd, false)
2017-10-26 23:43:31 +03:00
unlock()
2017-10-31 00:02:10 +03:00
c.action = events.Detached(c.id, c)
2017-10-26 23:43:31 +03:00
lock()
if c.action == Shutdown {
goto fail
}
goto next
}
}
syscall.Close(c.fd)
if events.Closed != nil {
unlock()
2017-11-01 03:44:57 +03:00
action := events.Closed(c.id, c.err)
2017-10-26 23:43:31 +03:00
lock()
if action == Shutdown {
c.action = Shutdown
}
}
if c.action == Shutdown {
err = nil
goto fail
}
goto next
fail:
unlock()
return err
next:
}
unlock()
}
}
2017-11-07 16:49:33 +03:00
func getlocaladdr(fd int) net.Addr {
2017-11-07 19:52:16 +03:00
if fd == 0 {
return nil
}
2017-10-29 23:09:40 +03:00
sa, _ := syscall.Getsockname(fd)
2017-11-07 16:49:33 +03:00
return getaddr(sa)
2017-10-29 23:09:40 +03:00
}
2017-11-07 16:49:33 +03:00
func getremoteaddr(fd int) net.Addr {
2017-11-07 19:52:16 +03:00
if fd == 0 {
return nil
}
2017-11-07 16:49:33 +03:00
sa, _ := syscall.Getpeername(fd)
return getaddr(sa)
}
func getaddr(sa syscall.Sockaddr) net.Addr {
switch sa := sa.(type) {
default:
return nil
case *syscall.SockaddrInet4:
return &net.TCPAddr{IP: net.IP(sa.Addr[:]), Port: sa.Port}
case *syscall.SockaddrInet6:
return &net.TCPAddr{IP: net.IP(sa.Addr[:]), Port: sa.Port, Zone: strconv.FormatInt(int64(sa.ZoneId), 10)}
case *syscall.SockaddrUnix:
return &net.UnixAddr{Net: "unix", Name: sa.Name}
2017-10-26 23:43:31 +03:00
}
}
2017-11-07 19:52:16 +03:00
// resolve resolves an evio address and retuns a sockaddr for socket
// connection to external servers.
func resolve(addr string) (sa syscall.Sockaddr, err error) {
network, address, _ := parseAddr(addr)
var taddr net.Addr
switch network {
default:
return nil, net.UnknownNetworkError(network)
case "unix":
taddr = &net.UnixAddr{Net: "unix", Name: address}
case "tcp", "tcp4", "tcp6":
// use the stdlib resolver because it's good.
taddr, err = net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
}
switch taddr := taddr.(type) {
case *net.UnixAddr:
sa = &syscall.SockaddrUnix{Name: taddr.Name}
case *net.TCPAddr:
if len(taddr.IP) == 4 {
var sa4 syscall.SockaddrInet4
copy(sa4.Addr[:], taddr.IP[:])
sa4.Port = taddr.Port
sa = &sa4
} else if len(taddr.IP) == 16 {
var sa6 syscall.SockaddrInet6
copy(sa6.Addr[:], taddr.IP[:])
sa6.Port = taddr.Port
sa = &sa6
}
}
return sa, nil
}