Last active
March 27, 2021 12:17
-
-
Save erikdubbelboer/3eebecc551245a3b1d5f06a9ef433654 to your computer and use it in GitHub Desktop.
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) | |
} | |
conn, err := ln.Accept() | |
if err != nil { | |
panic(err) | |
} | |
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 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/http" | |
"os" | |
"time" | |
) | |
func serve(w http.ResponseWriter, r *http.Request) { | |
f, err := os.Open("big.file") | |
if err != nil { | |
panic(err) | |
} | |
if _, err = io.Copy(w, f); err != nil { | |
panic(err) | |
} | |
if err := f.Close(); err != nil { | |
panic(err) | |
} | |
} | |
func main() { | |
go func() { | |
if err := http.ListenAndServe(":8081", http.HandlerFunc(serve)); err != nil { | |
panic(err) | |
} | |
}() | |
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) | |
} | |
client := &http.Client{ | |
Transport: &http.Transport{ | |
MaxIdleConnsPerHost: 1024, | |
}, | |
} | |
for i := 0; i < 10; i++ { | |
start := time.Now() | |
resp, err := client.Get("http://localhost:8081/") | |
if err != nil { | |
panic(err) | |
} | |
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil { | |
panic(err) | |
} | |
if err := resp.Body.Close(); err != nil { | |
panic(err) | |
} | |
fmt.Println(time.Since(start)) | |
time.Sleep(time.Second) | |
client.CloseIdleConnections() | |
} | |
} |
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
#include <fcntl.h> | |
#include <netinet/in.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/sendfile.h> | |
#include <unistd.h> | |
int main() { | |
int ln = socket(AF_INET, SOCK_STREAM, 0); | |
int enable = 1; | |
if (setsockopt(ln, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) { | |
return -1; | |
} | |
struct sockaddr_in addr; | |
memset(&addr, '0', sizeof(addr)); | |
addr.sin_family = AF_INET; | |
addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
addr.sin_port = htons(8081); | |
bind(ln, (struct sockaddr*)&addr, sizeof(addr)); | |
if (listen(ln, 10) == -1) { | |
return -1; | |
} | |
int conn = accept(ln, NULL, NULL); | |
while (1) { | |
unsigned char buff[1]; | |
int nread = read(conn, buff, 1); | |
if (nread != 1) { | |
break; | |
} | |
int fd = open("big.file", O_RDONLY); | |
if (fd < 0) { | |
return -1; | |
} | |
if (sendfile(conn, fd, NULL, 1024*100) != 1024*100) { | |
return -1; | |
} | |
if (close(fd) != 0) { | |
return -1; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment