mirror of
https://github.com/buger/goreplay.git
synced 2024-04-21 12:32:02 +00:00
### Reducing CPU context switching and number of goroutines. Packet capture and packet processing now use only two goroutines which helps to minimize CPU context switches. Spawning too many goroutines is harmful here. ### Optimized packet capture - allocated memory only when required, and only for data which is used Using ZeroCopy methods from libpcap library to avoid unnecessary allocations. Now memory gets allocated ONLY for the valid packets, and only for the packets which have the data. E.g. no SYN/FIN packets are used now. Additionally we now use `sync.Pool` for re-using packet objects, which helps to re-use already allocated memory. ### Simplification and optimization of request/response detection There is no SYN/FIN packets anymore etc. Now only packet payload is used to detect start and end of the packet. More over payload detection now does not require generating a total “message” buffer, and works with individual packet payloads. Message payloads now concatenated from packets only in the end when message is dispatched. Also, before checking if message is complete, added additional check if all received packets in the valid order, e.g. if their SEQ is valid, and no packets are missing. Reworked chunked encoding validation, and now it does not need expensive operation of re-calculating all the chunks. Now it “trust” that client gives valid chunk body, check if packets are in the right order (e.g. SEQ match), and checks if message ends with the right suffix. All is done with 0 allocations. Parsing all Headers using `proto.GetHeaders` was proved to be very slow. Now we only parse the headers we need(and do it only once). Packets gets matched together using ACK, which on high RPS removed chances of duplicating IDs. Additionally, even if packets are received out of order, now it will properly sort them, before dispatching the message. ### Changes in ID generation algorithm Message ID generation and relations between request and response IDs is fully rewritten. Responses now do not have to lookup for request data in order to get the same ID. ID no rely on the fact that SEQ of the first packet of the response should be the same as ACK of the request. If previously Message ID contained random values, like current timestamp, now it has a consistent algorithm which is based on TCP stream id (SrcPort + DstPort + SrcIP/DstIP) and current ACK/SEQ number (to distinguish multiple messages within the same stream). ### BPF filter optimizations When tracking response it now uses a more accurate BPF rule to filter only needed traffic. ### Misc The packet code is now fully moved to tcp/Packet, so packet processing done only once in one place. TCP output now has a 5 second timeout, and has a proper Close method. Fully switching to go modules and removing vendoring.
126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
// Gor is simple http traffic replication tool written in Go. Its main goal to replay traffic from production servers to staging and dev environments.
|
|
// Now you can test your code on real user sessions in an automated and repeatable fashion.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
_ "net/http/pprof"
|
|
"os"
|
|
"os/signal"
|
|
"runtime"
|
|
_ "runtime/debug"
|
|
"runtime/pprof"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
memprofile = flag.String("memprofile", "", "write memory profile to this file")
|
|
)
|
|
|
|
func loggingMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
rb, _ := httputil.DumpRequest(r, false)
|
|
log.Println(string(rb))
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
if os.Getenv("GOMAXPROCS") == "" {
|
|
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
|
|
}
|
|
|
|
args := os.Args[1:]
|
|
var plugins *InOutPlugins
|
|
if len(args) > 0 && args[0] == "file-server" {
|
|
if len(args) != 2 {
|
|
log.Fatal("You should specify port and IP (optional) for the file server. Example: `gor file-server :80`")
|
|
}
|
|
dir, _ := os.Getwd()
|
|
|
|
Debug(0, "Started example file server for current directory on address ", args[1])
|
|
|
|
log.Fatal(http.ListenAndServe(args[1], loggingMiddleware(http.FileServer(http.Dir(dir)))))
|
|
} else {
|
|
flag.Parse()
|
|
checkSettings()
|
|
plugins = NewPlugins()
|
|
}
|
|
|
|
log.Printf("[PPID %d and PID %d] Version:%s\n", os.Getppid(), os.Getpid(), VERSION)
|
|
|
|
if len(plugins.Inputs) == 0 || len(plugins.Outputs) == 0 {
|
|
log.Fatal("Required at least 1 input and 1 output")
|
|
}
|
|
|
|
if *memprofile != "" {
|
|
profileMEM(*memprofile)
|
|
}
|
|
|
|
if *cpuprofile != "" {
|
|
profileCPU(*cpuprofile)
|
|
}
|
|
|
|
if Settings.Pprof != "" {
|
|
go func() {
|
|
log.Println(http.ListenAndServe(Settings.Pprof, nil))
|
|
}()
|
|
}
|
|
|
|
closeCh := make(chan int)
|
|
emitter := NewEmitter()
|
|
go emitter.Start(plugins, Settings.Middleware)
|
|
if Settings.ExitAfter > 0 {
|
|
log.Printf("Running gor for a duration of %s\n", Settings.ExitAfter)
|
|
|
|
time.AfterFunc(Settings.ExitAfter, func() {
|
|
log.Printf("gor run timeout %s\n", Settings.ExitAfter)
|
|
close(closeCh)
|
|
})
|
|
}
|
|
c := make(chan os.Signal, 1)
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
exit := 0
|
|
select {
|
|
case <-c:
|
|
exit = 1
|
|
case <-closeCh:
|
|
exit = 0
|
|
}
|
|
emitter.Close()
|
|
os.Exit(exit)
|
|
}
|
|
|
|
func profileCPU(cpuprofile string) {
|
|
if cpuprofile != "" {
|
|
f, err := os.Create(cpuprofile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
pprof.StartCPUProfile(f)
|
|
|
|
time.AfterFunc(30*time.Second, func() {
|
|
pprof.StopCPUProfile()
|
|
f.Close()
|
|
})
|
|
}
|
|
}
|
|
|
|
func profileMEM(memprofile string) {
|
|
if memprofile != "" {
|
|
f, err := os.Create(memprofile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
time.AfterFunc(30*time.Second, func() {
|
|
pprof.WriteHeapProfile(f)
|
|
f.Close()
|
|
})
|
|
}
|
|
}
|