Skip to content

Commit

Permalink
Fix bugs in reactor network model
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Sep 14, 2019
1 parent 54d17b3 commit 28f1132
Show file tree
Hide file tree
Showing 574 changed files with 285,231 additions and 590 deletions.
2 changes: 0 additions & 2 deletions benchmarks/README.md
Expand Up @@ -4,14 +4,12 @@ Required tools:

- [bombardier](https://github.com/codesenberg/bombardier) for HTTP
- [tcpkali](https://github.com/machinezone/tcpkali) for Echo
- [Redis](http://redis.io) for Redis

Required Go packages:

```
go get gonum.org/v1/plot/...
go get -u github.com/valyala/fasthttp
go get -u github.com/tidwall/redcon
```

And of course [Go](https://golang.org) is required.
Expand Down
8 changes: 1 addition & 7 deletions benchmarks/analyze.go
Expand Up @@ -42,7 +42,7 @@ func autoplot() {
}

func analyze() {
lines := readlines("out/http.txt", "out/echo.txt", "out/redis1.txt", "out/redis8.txt", "out/redis16.txt")
lines := readlines("out/http.txt", "out/echo.txt")
var err error
for _, line := range lines {
rlines := strings.Split(line, "\r")
Expand Down Expand Up @@ -91,12 +91,6 @@ func analyze() {
must(err)
output()
}
case strings.HasPrefix(category, "redis"):
if strings.HasPrefix(line, "PING_INLINE: ") {
rate, err = strconv.ParseFloat(strings.Split(strings.Split(line, ": ")[1], " ")[0], 64)
must(err)
output()
}
}
}
}
Expand Down
43 changes: 0 additions & 43 deletions benchmarks/bench-redis.sh

This file was deleted.

3 changes: 0 additions & 3 deletions benchmarks/bench.sh
Expand Up @@ -8,8 +8,5 @@ mkdir -p out/

./bench-http.sh 2>&1 | tee out/http.txt
./bench-echo.sh 2>&1 | tee out/echo.txt
./bench-redis.sh 1 2>&1 | tee out/redis1.txt
./bench-redis.sh 8 2>&1 | tee out/redis8.txt
./bench-redis.sh 16 2>&1 | tee out/redis16.txt

go run analyze.go
Binary file removed benchmarks/out/echo.png
Binary file not shown.
34 changes: 0 additions & 34 deletions benchmarks/out/echo.txt

This file was deleted.

Binary file removed benchmarks/out/http.png
Binary file not shown.
49 changes: 0 additions & 49 deletions benchmarks/out/http.txt

This file was deleted.

28 changes: 0 additions & 28 deletions benchmarks/out/redis1.txt

This file was deleted.

28 changes: 0 additions & 28 deletions benchmarks/out/redis16.txt

This file was deleted.

28 changes: 0 additions & 28 deletions benchmarks/out/redis8.txt

This file was deleted.

Binary file removed benchmarks/out/redis_pipeline_1.png
Binary file not shown.
Binary file removed benchmarks/out/redis_pipeline_16.png
Binary file not shown.
Binary file removed benchmarks/out/redis_pipeline_8.png
Binary file not shown.
20 changes: 19 additions & 1 deletion conn_unix.go
Expand Up @@ -8,6 +8,7 @@ package gnet
import (
"io"
"net"
"syscall"

"github.com/panjf2000/gnet/ringbuffer"
"golang.org/x/sys/unix"
Expand All @@ -28,14 +29,31 @@ type conn struct {
loop *loop // connected loop
}

func (c *conn) sendOut(buf []byte) {
if !c.outBuf.IsFull() && !c.outBuf.IsEmpty() {
_, _ = c.outBuf.Write(buf)
return
}

n, err := syscall.Write(c.fd, buf)
if err != nil {
_, _ = c.outBuf.Write(buf)
return
}

if n < len(buf) {
_, _ = c.outBuf.Write(buf[n:])
}
}

func (c *conn) Context() interface{} { return c.ctx }
func (c *conn) SetContext(ctx interface{}) { c.ctx = ctx }
func (c *conn) AddrIndex() int { return c.addrIndex }
func (c *conn) LocalAddr() net.Addr { return c.localAddr }
func (c *conn) RemoteAddr() net.Addr { return c.remoteAddr }
func (c *conn) Wake() {
if c.loop != nil {
sniffError(c.loop.poll.Trigger(c))
sniffError(c.loop.poller.Trigger(c))
}
}

Expand Down
18 changes: 7 additions & 11 deletions examples/echo-server/main.go
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/panjf2000/gnet"
"github.com/panjf2000/gnet/ringbuffer"
)

func main() {
Expand All @@ -19,14 +20,12 @@ func main() {
var udp bool
var trace bool
var reuseport bool
var stdlib bool

flag.IntVar(&port, "port", 5000, "server port")
flag.BoolVar(&udp, "udp", false, "listen on udp")
flag.BoolVar(&reuseport, "reuseport", false, "reuseport (SO_REUSEPORT)")
flag.BoolVar(&trace, "trace", false, "print packets to console")
flag.IntVar(&loops, "loops", 0, "num loops")
flag.BoolVar(&stdlib, "stdlib", false, "use stdlib")
flag.Parse()

var events gnet.Events
Expand All @@ -36,24 +35,21 @@ func main() {
if reuseport {
log.Printf("reuseport")
}
if stdlib {
log.Printf("stdlib")
}
return
}
events.React = func(c gnet.Conn, in []byte) (out []byte, action gnet.Action) {
events.React = func(c gnet.Conn, inBuf *ringbuffer.RingBuffer) (out []byte, action gnet.Action) {
n := inBuf.Length()
defer inBuf.Move(n)
//out = inBuf.Bytes()
defer ringbuffer.Recycle(out)
if trace {
log.Printf("%s", strings.TrimSpace(string(in)))
log.Printf("%s", strings.TrimSpace(string(out)))
}
out = in
return
}
scheme := "tcp"
if udp {
scheme = "udp"
}
if stdlib {
scheme += "-net"
}
log.Fatal(gnet.Serve(events, fmt.Sprintf("%s://:%d?reuseport=%t", scheme, port, reuseport)))
}

0 comments on commit 28f1132

Please sign in to comment.