Last active
July 4, 2023 03:04
-
-
Save lesismal/d63098add20868d2b71a2168483ef891 to your computer and use it in GitHub Desktop.
nbio sendfile on linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net" | |
"os" | |
"time" | |
"github.com/lesismal/nbio" | |
) | |
func server() { | |
g := nbio.NewGopher(nbio.Config{ | |
Network: "tcp", | |
Addrs: []string{"localhost:8081"}, | |
}) | |
g.OnData(func(c *nbio.Conn, data []byte) { | |
fd, err := os.Open("big.file") | |
if err != nil { | |
panic(err) | |
} | |
if _, err = c.Sendfile(fd, 0); err != nil { | |
panic(err) | |
} | |
if err := fd.Close(); err != nil { | |
panic(err) | |
} | |
}) | |
err := g.Start() | |
if err != nil { | |
fmt.Printf("nbio.Start failed: %v\n", err) | |
return | |
} | |
} | |
func main() { | |
if len(os.Args) < 2 || os.Args[1] != "noserver" { | |
go server() | |
} | |
time.Sleep(time.Millisecond * 100) // Give the server some time to start. | |
if err := ioutil.WriteFile("big.file", make([]byte, 1024*100), 0666); err != nil { | |
panic(err) | |
} | |
conn, err := net.Dial("tcp", "localhost:8081") | |
if err != nil { | |
panic(err) | |
} | |
buf := make([]byte, 1024*100) | |
for i := 0; i < 10; i++ { | |
start := time.Now() | |
if _, err := conn.Write([]byte{1}); err != nil { | |
panic(err) | |
} | |
if _, err := io.ReadFull(conn, buf); err != nil { | |
panic(err) | |
} | |
fmt.Println(time.Since(start)) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net" | |
"os" | |
"time" | |
) | |
func server() { | |
ln, err := net.Listen("tcp", ":8081") | |
if err != nil { | |
panic(err) | |
} | |
for { | |
conn, err := ln.Accept() | |
if err != nil { | |
panic(err) | |
} | |
go func() { | |
for { | |
buf := make([]byte, 1) | |
if _, err := conn.Read(buf); err != nil { | |
return | |
} | |
fd, err := os.Open("big.file") | |
if err != nil { | |
panic(err) | |
} | |
if _, err = io.Copy(conn, fd); err != nil { | |
panic(err) | |
} | |
if err := fd.Close(); err != nil { | |
panic(err) | |
} | |
} | |
}() | |
} | |
} | |
func readfile(conn *net.TCPConn) { | |
buf := make([]byte, 1024*100) | |
for i := 0; i < 10; i++ { | |
start := time.Now() | |
if _, err := conn.Write([]byte{1}); err != nil { | |
panic(err) | |
} | |
if _, err := io.ReadFull(conn, buf); err != nil { | |
panic(err) | |
} | |
fmt.Println(time.Since(start)) | |
} | |
} | |
func main() { | |
if len(os.Args) < 2 || os.Args[1] != "noserver" { | |
go server() | |
} | |
time.Sleep(time.Millisecond * 100) // Give the server some time to start. | |
if err := ioutil.WriteFile("big.file", make([]byte, 1024*100), 0666); err != nil { | |
panic(err) | |
} | |
conn, err := net.Dial("tcp", "localhost:8081") | |
if err != nil { | |
panic(err) | |
} | |
tcpConn := conn.(*net.TCPConn) | |
fmt.Println("default read buffer:") | |
readfile(tcpConn) | |
fmt.Println("\nread buffer 1024*1000:") | |
conn, err = net.Dial("tcp", "localhost:8081") | |
if err != nil { | |
panic(err) | |
} | |
tcpConn = conn.(*net.TCPConn) | |
tcpConn.SetReadBuffer(1024 * 1000) | |
readfile(tcpConn) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment