Golang 中国

evio是一个基于单线程事件驱动的网络框架,它很小而且相比Go标准的net package更快。底层使用epoll和kqueue系统调度实现。
https://github.com/tidwall/evio

功能与特点:
1、高性能的单线程事件驱动
2、简单的API
3、底内存消耗
4、支持tcp4,tcp6和unix sockets
5、允许多网络(多个监听端口,多种协议)绑定同一个事件驱动
6、可扩展性的事件触发
7、拦截非epoll/kqueue操作系统由net package模拟的事件
8、为耗时较长的业务处理提供连接唤醒功能
9、可绑定第三方连接实现代理

事件:
Serving:当server准备接口连接
Opened: 当一个连接开启之后
Closed: 当一个连接之后关闭
Detach:当使用Detach操作脱离连接
Data:当server从client接收到新的数据包
Prewrite:在server写回数据之前
Postwrite:在server写回数据之后
Tick:在server开启之后并且每隔一断时间都会触发

安装:
$ go get -u github.com/tidwall/evio

例子:
开启一个监听服务

package main

import "github.com/tidwall/evio"

func main() {
    var events evio.Events
    //自定义Data事件处理
    events.Data = func(id int, in []byte) (out []byte, action evio.Action) {
        out = in
        return
    }
    //监听5000端口
    if err := evio.Serve(events, "tcp://localhost:5000"); err != nil {
        panic(err.Error())
    }
}

绑定第三方网络

var srv evio.Server
var mu sync.Mutex
var execs = make(map[int]int)

events.Serving = func(srvin evio.Server) (action evio.Action) {
    srv = srvin // hang on to the server control, which has the Dial function
    return
}
events.Data = func(id int, in []byte) (out []byte, action evio.Action) {
    if string(in) == "dial\r\n" {
        id := srv.Dial("tcp://google.com:80")
        // We now established an outbound connection to google.
        // Treat it like you would incoming connection.
    } else {
        out = in
    }
    return
}

http服务

// 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.

package main

import (
    "bytes"
    "crypto/tls"
    "flag"
    "fmt"
    "io"
    "log"
    "os"
    "strconv"
    "strings"
    "time"

    "github.com/tidwall/evio"
)

var res string

type request struct {
    proto, method string
    path, query   string
    head, body    string
    remoteAddr    string
}

type conn struct {
    info evio.Info
    is   evio.InputStream
}

func main() {
    var port int
    var tlsport int
    var tlspem string
    var aaaa bool
    var noparse bool
    var unixsocket string
    var stdlib bool
    flag.StringVar(&unixsocket, "unixsocket", "", "unix socket")
    flag.IntVar(&port, "port", 8080, "server port")
    flag.IntVar(&tlsport, "tlsport", 4443, "tls port")
    flag.StringVar(&tlspem, "tlscert", "", "tls pem cert/key file")
    flag.BoolVar(&aaaa, "aaaa", false, "aaaaa....")
    flag.BoolVar(&noparse, "noparse", true, "do not parse requests")
    flag.BoolVar(&stdlib, "stdlib", false, "use stdlib")
    flag.Parse()

    if os.Getenv("NOPARSE") == "1" {
        noparse = true
    }

    if aaaa {
        res = strings.Repeat("a", 1024)
    } else {
        res = "Hello World!\r\n"
    }

    var events evio.Events
    var conns = make(map[int]*conn)

    events.Serving = func(server evio.Server) (action evio.Action) {
        log.Printf("http server started on port %d", port)
        if tlspem != "" {
            log.Printf("https server started on port %d", tlsport)
        }
        if unixsocket != "" {
            log.Printf("http server started at %s", unixsocket)
        }
        if stdlib {
            log.Printf("stdlib")
        }
        return
    }

    events.Opened = func(id int, info evio.Info) (out []byte, opts evio.Options, action evio.Action) {
        conns[id] = &conn{info: info}
        log.Printf("opened: %d: laddr: %v: raddr: %v", id, info.LocalAddr, info.RemoteAddr)

        // println(info.LocalAddr.(*net.TCPAddr).Zone)
        // fmt.Printf("%#v\n", info.LocalAddr)
        // fmt.Printf("%#v\n", (&net.TCPAddr{IP: make([]byte, 16)}))
        return
    }

    events.Closed = func(id int, err error) (action evio.Action) {
        c := conns[id]
        log.Printf("closed: %d: %s: %s", id, c.info.LocalAddr.String(), c.info.RemoteAddr.String())
        delete(conns, id)
        return
    }

    events.Data = func(id int, in []byte) (out []byte, action evio.Action) {
        if in == nil {
            return
        }
        c := conns[id]
        data := c.is.Begin(in)
        if noparse && bytes.Contains(data, []byte("\r\n\r\n")) {
            // for testing minimal single packet request -> response.
            out = appendresp(nil, "200 OK", "", res)
            return
        }
        // process the pipeline
        var req request
        for {
            leftover, err := parsereq(data, &req)
            if err != nil {
                // bad thing happened
                out = appendresp(out, "500 Error", "", err.Error()+"\n")
                action = evio.Close
                break
            } else if len(leftover) == len(data) {
                // request not ready, yet
                break
            }
            // handle the request
            req.remoteAddr = c.info.RemoteAddr.String()
            out = appendhandle(out, &req)
            data = leftover
        }
        c.is.End(data)
        return
    }
    var ssuf string
    if stdlib {
        ssuf = "-net"
    }
    // We at least want the single http address.
    addrs := []string{fmt.Sprintf("tcp"+ssuf+"://:%d", port)}
    if tlspem != "" {
        // load the cert and key pair from the concat'd pem file.
        cer, err := tls.LoadX509KeyPair(tlspem, tlspem)
        if err != nil {
            log.Fatal(err)
        }
        config := &tls.Config{Certificates: []tls.Certificate{cer}}
        // Update the address list to include https.
        addrs = append(addrs, fmt.Sprintf("tcp"+ssuf+"://:%d", tlsport))

        // TLS translate the events
        events = evio.Translate(events,
            func(id int, info evio.Info) bool {
                // only translate for the second address.
                return info.AddrIndex == 1
            },
            func(id int, rw io.ReadWriter) io.ReadWriter {
                // Use the standard Go crypto/tls package and create a tls.Conn
                // from the provided io.ReadWriter. Here we use the handy
                // evio.NopConn utility to create a barebone net.Conn in order
                // for the tls.Server to accept the connection.
                return tls.Server(evio.NopConn(rw), config)
            },
        )
    }
    if unixsocket != "" {
        addrs = append(addrs, fmt.Sprintf("unix"+ssuf+"://%s", unixsocket))
    }
    // Start serving!
    log.Fatal(evio.Serve(events, addrs...))
}

// appendhandle handles the incoming request and appends the response to
// the provided bytes, which is then returned to the caller.
func appendhandle(b []byte, req *request) []byte {
    return appendresp(b, "200 OK", "", res)
}

// appendresp will append a valid http response to the provide bytes.
// The status param should be the code plus text such as "200 OK".
// The head parameter should be a series of lines ending with "\r\n" or empty.
func appendresp(b []byte, status, head, body string) []byte {
    b = append(b, "HTTP/1.1"...)
    b = append(b, ' ')
    b = append(b, status...)
    b = append(b, '\r', '\n')
    b = append(b, "Server: evio\r\n"...)
    b = append(b, "Date: "...)
    b = time.Now().AppendFormat(b, "Mon, 02 Jan 2006 15:04:05 GMT")
    b = append(b, '\r', '\n')
    if len(body) > 0 {
        b = append(b, "Content-Length: "...)
        b = strconv.AppendInt(b, int64(len(body)), 10)
        b = append(b, '\r', '\n')
    }
    b = append(b, head...)
    b = append(b, '\r', '\n')
    if len(body) > 0 {
        b = append(b, body...)
    }
    return b
}

// parsereq is a very simple http request parser. This operation
// waits for the entire payload to be buffered before returning a
// valid request.
func parsereq(data []byte, req *request) (leftover []byte, err error) {
    sdata := string(data)
    var i, s int
    var top string
    var clen int
    var q = -1
    // method, path, proto line
    for ; i < len(sdata); i++ {
        if sdata[i] == ' ' {
            req.method = sdata[s:i]
            for i, s = i+1, i+1; i < len(sdata); i++ {
                if sdata[i] == '?' && q == -1 {
                    q = i - s
                } else if sdata[i] == ' ' {
                    if q != -1 {
                        req.path = sdata[s:q]
                        req.query = req.path[q+1 : i]
                    } else {
                        req.path = sdata[s:i]
                    }
                    for i, s = i+1, i+1; i < len(sdata); i++ {
                        if sdata[i] == '\n' && sdata[i-1] == '\r' {
                            req.proto = sdata[s:i]
                            i, s = i+1, i+1
                            break
                        }
                    }
                    break
                }
            }
            break
        }
    }
    if req.proto == "" {
        return data, fmt.Errorf("malformed request")
    }
    top = sdata[:s]
    for ; i < len(sdata); i++ {
        if i > 1 && sdata[i] == '\n' && sdata[i-1] == '\r' {
            line := sdata[s : i-1]
            s = i + 1
            if line == "" {
                req.head = sdata[len(top)+2 : i+1]
                i++
                if clen > 0 {
                    if len(sdata[i:]) < clen {
                        break
                    }
                    req.body = sdata[i : i+clen]
                    i += clen
                }
                return data[i:], nil
            }
            if strings.HasPrefix(line, "Content-Length:") {
                n, err := strconv.ParseInt(strings.TrimSpace(line[len("Content-Length:"):]), 10, 64)
                if err == nil {
                    clen = int(n)
                }
            }
        }
    }
    // not enough data
    return data, nil
}

性能对比图



可能有部分翻译或理解上的错误欢迎指正

0 回复
需要 登录 后方可回复, 如果你还没有账号你可以 注册 一个帐号。