add lock free queue

This commit is contained in:
ffdfgdfg
2019-10-21 11:55:29 +08:00
parent c2f4510a0f
commit 23b023c562
5 changed files with 419 additions and 131 deletions

View File

@@ -3,13 +3,16 @@ package mux
import (
"bufio"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
"strconv"
"sync"
"testing"
"time"
"unsafe"
"github.com/astaxie/beego/logs"
"github.com/cnlh/nps/lib/common"
@@ -30,20 +33,22 @@ func TestNewMux(t *testing.T) {
go func() {
m2 := NewMux(conn2, "tcp")
for {
logs.Warn("npc starting accept")
//logs.Warn("npc starting accept")
c, err := m2.Accept()
if err != nil {
logs.Warn(err)
continue
}
logs.Warn("npc accept success ")
//logs.Warn("npc accept success ")
c2, err := net.Dial("tcp", "127.0.0.1:80")
if err != nil {
logs.Warn(err)
c.Close()
continue
}
go func(c2 net.Conn, c net.Conn) {
//c2.(*net.TCPConn).SetReadBuffer(0)
//c2.(*net.TCPConn).SetReadBuffer(0)
go func(c2 net.Conn, c *conn) {
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
@@ -51,7 +56,7 @@ func TestNewMux(t *testing.T) {
if err != nil {
c2.Close()
c.Close()
logs.Warn("close npc by copy from nps", err)
logs.Warn("close npc by copy from nps", err, c.connId)
}
wg.Done()
}()
@@ -61,13 +66,13 @@ func TestNewMux(t *testing.T) {
if err != nil {
c2.Close()
c.Close()
logs.Warn("close npc by copy from server", err)
logs.Warn("close npc by copy from server", err, c.connId)
}
wg.Done()
}()
logs.Warn("npc wait")
//logs.Warn("npc wait")
wg.Wait()
}(c2, c)
}(c2, c.(*conn))
}
}()
@@ -78,42 +83,46 @@ func TestNewMux(t *testing.T) {
logs.Warn(err)
}
for {
logs.Warn("nps starting accept")
conn, err := l.Accept()
//logs.Warn("nps starting accept")
conns, err := l.Accept()
if err != nil {
logs.Warn(err)
continue
}
logs.Warn("nps accept success starting new conn")
//conns.(*net.TCPConn).SetReadBuffer(0)
//conns.(*net.TCPConn).SetReadBuffer(0)
//logs.Warn("nps accept success starting new conn")
tmpCpnn, err := m1.NewConn()
if err != nil {
logs.Warn("nps new conn err ", err)
continue
}
logs.Warn("nps new conn success ", tmpCpnn.connId)
go func(tmpCpnn net.Conn, conn net.Conn) {
go func(tmpCpnn *conn, conns net.Conn) {
go func() {
_, err := common.CopyBuffer(tmpCpnn, conn)
_, err := common.CopyBuffer(tmpCpnn, conns)
if err != nil {
conn.Close()
conns.Close()
tmpCpnn.Close()
logs.Warn("close nps by copy from user")
logs.Warn("close nps by copy from user", tmpCpnn.connId, err)
}
}()
//time.Sleep(time.Second)
_, err = common.CopyBuffer(conn, tmpCpnn)
_, err = common.CopyBuffer(conns, tmpCpnn)
if err != nil {
conn.Close()
conns.Close()
tmpCpnn.Close()
logs.Warn("close nps by copy from npc ")
logs.Warn("close nps by copy from npc ", tmpCpnn.connId, err)
}
}(tmpCpnn, conn)
}(tmpCpnn, conns)
}
}()
go NewLogServer()
time.Sleep(time.Second * 5)
//go test_request()
//for i:=0;i<1000;i++ {
// go test_raw(i)
//}
for {
time.Sleep(time.Second * 5)
@@ -168,23 +177,40 @@ Connection: keep-alive
}
}
func test_raw() {
conn, _ := net.Dial("tcp", "127.0.0.1:7777")
for {
conn.Write([]byte(`GET /videojs5/test HTTP/1.1
func test_raw(k int) {
for i := 0; i < 1; i++ {
ti := time.Now()
conn, _ := net.Dial("tcp", "127.0.0.1:7777")
tid := time.Now()
conn.Write([]byte(`GET / HTTP/1.1
Host: 127.0.0.1:7777
Connection: keep-alive
`))
buf := make([]byte, 1000000)
n, err := conn.Read(buf)
tiw := time.Now()
buf := make([]byte, 3572)
n, err := io.ReadFull(conn, buf)
//n, err := conn.Read(buf)
if err != nil {
logs.Warn("close by read response err", err)
break
}
logs.Warn(n, string(buf[:50]), "\n--------------\n", string(buf[n-50:n]))
time.Sleep(time.Second)
//logs.Warn(n, string(buf[:50]), "\n--------------\n", string(buf[n-50:n]))
//time.Sleep(time.Second)
err = conn.Close()
if err != nil {
logs.Warn("close conn err ", err)
}
now := time.Now()
du := now.Sub(ti).Seconds()
dud := now.Sub(tid).Seconds()
duw := now.Sub(tiw).Seconds()
if du > 1 {
logs.Warn("duration long", du, dud, duw, k, i)
}
if n != 3572 {
logs.Warn("n loss", n, string(buf))
}
}
}
@@ -199,3 +225,53 @@ func TestNewConn(t *testing.T) {
logs.Warn(copy(buf[:3], b), len(buf), cap(buf))
logs.Warn(len(buf), buf[0])
}
func TestDQueue(t *testing.T) {
logs.EnableFuncCallDepth(true)
logs.SetLogFuncCallDepth(3)
d := new(bufDequeue)
d.vals = make([]unsafe.Pointer, 8)
go func() {
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
logs.Warn(i)
logs.Warn(d.popTail())
}
}()
go func() {
time.Sleep(time.Second)
for i := 0; i < 10; i++ {
data := "test"
go logs.Warn(i, unsafe.Pointer(&data), d.pushHead(unsafe.Pointer(&data)))
}
}()
time.Sleep(time.Second * 3)
}
func TestChain(t *testing.T) {
logs.EnableFuncCallDepth(true)
logs.SetLogFuncCallDepth(3)
d := new(bufChain)
d.new(256)
go func() {
time.Sleep(time.Second)
for i := 0; i < 1000; i++ {
unsa, ok := d.popTail()
str := (*string)(unsa)
if ok {
logs.Warn(i, str, *str, ok)
} else {
logs.Warn("nil", i, ok)
}
}
}()
go func() {
time.Sleep(time.Second)
for i := 0; i < 1000; i++ {
data := "test " + strconv.Itoa(i)
logs.Warn(data, unsafe.Pointer(&data))
go d.pushHead(unsafe.Pointer(&data))
}
}()
time.Sleep(time.Second * 10)
}