GithubHelp home page GithubHelp logo

Comments (3)

lesismal avatar lesismal commented on June 15, 2024 2

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

from nbio.

ouvaa avatar ouvaa commented on June 15, 2024

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

from nbio.

lesismal avatar lesismal commented on June 15, 2024

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!

from nbio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.