Closed ouvaa closed 6 months ago
You can set Listen func and NListener like this:
package main
import (
"fmt"
"net/http"
"time"
"github.com/lesismal/nbio/nbhttp"
reuseport "github.com/libp2p/go-reuseport"
)
func onEcho(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(time.Now().Format("20060102 15:04:05")))
}
func main() {
mux := &http.ServeMux{}
mux.HandleFunc("/", onEcho)
engine := nbhttp.NewServer(nbhttp.Config{
Network: "tcp",
Addrs: []string{"localhost:8080"},
Listen: reuseport.Listen, // set reuseport listen
// NListener: 2, // how many goroutine to handle Accept for each addr, if not set, 1 goroutine by default
Handler: mux,
})
err := engine.Start()
if err != nil {
fmt.Printf("nbio.Start failed: %v\n", err)
return
}
defer engine.Stop()
<-make(chan int)
}
amazing! thx. u should put somewhere easier to find :D i cant believe i'm the first to ask this question
Some other users asked about reuseport before, but in Chinese: https://github.com/lesismal/nbio/issues/285#issuecomment-1512696017 https://github.com/lesismal/nbio/issues/157#issuecomment-1047563475
I'll do some investment to provide more docs/faq, thx for your feedback bro!
as titled