Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use with so reuseport option for the http example? #406

Closed
ouvaa opened this issue Mar 21, 2024 · 3 comments
Closed

how to use with so reuseport option for the http example? #406

ouvaa opened this issue Mar 21, 2024 · 3 comments

Comments

@ouvaa
Copy link

ouvaa commented Mar 21, 2024

as titled

@lesismal
Copy link
Owner

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)
}

@ouvaa
Copy link
Author

ouvaa commented Mar 21, 2024

amazing! thx. u should put somewhere easier to find :D i cant believe i'm the first to ask this question

@ouvaa ouvaa closed this as completed Mar 21, 2024
@lesismal
Copy link
Owner

Some other users asked about reuseport before, but in Chinese:
#285 (comment)
#157 (comment)

I'll do some investment to provide more docs/faq, thx for your feedback bro!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants