**Summary of Changes** 1. Add an example that uses the write buffer pool The loop process of the websocket connection is inner the http handler at existing examples, This usage will cause the 8k buffer(4k read buffer + 4k write buffer) allocated by net.http can't be GC(Observed by heap profiling, see picture below) . The purpose of saving memory is not achieved even if the WriteBufferPool is used. In example bufferpool, server process websocket connection in a new goroutine, and the goroutine created by the net.http will exit, then the 8k buffer will be GC.  Co-authored-by: hakunaliu <hakunaliu@tencent.com> Co-authored-by: Corey Daley <cdaley@redhat.com>
56 lines
961 B
Go
56 lines
961 B
Go
//go:build ignore
|
|
// +build ignore
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
var addr = flag.String("addr", "localhost:8080", "http service address")
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
ReadBufferSize: 256,
|
|
WriteBufferSize: 256,
|
|
WriteBufferPool: &sync.Pool{},
|
|
}
|
|
|
|
func process(c *websocket.Conn) {
|
|
defer c.Close()
|
|
for {
|
|
_, message, err := c.ReadMessage()
|
|
if err != nil {
|
|
log.Println("read:", err)
|
|
break
|
|
}
|
|
log.Printf("recv: %s", message)
|
|
}
|
|
}
|
|
|
|
func handler(w http.ResponseWriter, r *http.Request) {
|
|
c, err := upgrader.Upgrade(w, r, nil)
|
|
if err != nil {
|
|
log.Print("upgrade:", err)
|
|
return
|
|
}
|
|
|
|
// Process connection in a new goroutine
|
|
go process(c)
|
|
|
|
// Let the http handler return, the 8k buffer created by it will be garbage collected
|
|
}
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
log.SetFlags(0)
|
|
http.HandleFunc("/ws", handler)
|
|
log.Fatal(http.ListenAndServe(*addr, nil))
|
|
}
|