func ExampleListen() {
	ln, err := reuseport.Listen("tcp4", "localhost:12345")
	if err != nil {
		log.Fatalf("error in reuseport listener: %s", err)
	}

	if err = fasthttp.Serve(ln, requestHandler); err != nil {
		log.Fatalf("error in fasthttp Server: %s", err)
	}
}
func getListener() net.Listener {
	if !*prefork {
		ln, err := net.Listen("tcp4", *addr)
		if err != nil {
			log.Fatal(err)
		}
		return ln
	}

	if !*child {
		children := make([]*exec.Cmd, runtime.NumCPU())
		for i := range children {
			if *affinity {
				children[i] = exec.Command(os.Args[0], "-prefork", "-child")
			} else {
				children[i] = exec.Command("taskset", "-c", fmt.Sprintf("%d", i), os.Args[0], "-prefork", "-child")
			}
			children[i].Stdout = os.Stdout
			children[i].Stderr = os.Stderr
			if err := children[i].Start(); err != nil {
				log.Fatal(err)
			}
		}
		for _, ch := range children {
			if err := ch.Wait(); err != nil {
				log.Print(err)
			}
		}
		os.Exit(0)
		panic("unreachable")
	}

	runtime.GOMAXPROCS(1)
	ln, err := reuseport.Listen("tcp4", *addr)
	if err != nil {
		log.Fatal(err)
	}
	return ln
}
Пример #3
0
func GetListener() net.Listener {
	if !*Prefork {
		runtime.GOMAXPROCS(runtime.NumCPU())
		ln, err := net.Listen("tcp4", *listenAddr)
		if err != nil {
			log.Fatal(err)
		}
		return ln
	}

	if !*child {
		children := make([]*exec.Cmd, runtime.NumCPU())
		for i := range children {
			children[i] = exec.Command(os.Args[0], "-prefork", "-child")
			children[i].Stdout = os.Stdout
			children[i].Stderr = os.Stderr
			if err := children[i].Start(); err != nil {
				log.Fatal(err)
			}
		}
		for _, ch := range children {
			if err := ch.Wait(); err != nil {
				log.Print(err)
			}
		}
		os.Exit(0)
		panic("unreachable")
	}

	runtime.GOMAXPROCS(1)
	ln, err := reuseport.Listen("tcp4", *listenAddr)
	if err != nil {
		log.Fatal(err)
	}
	return ln
}
Пример #4
0
func Boot() {
	// Create Router
	r := echo.New()

	// Route it!
	route(r)

	// Setup
	r.Use(middleware.CORSWithConfig(middleware.DefaultCORSConfig))
	r.Use(middleware.Recover())

	// Build port string from config
	port := fmt.Sprint(":" + strconv.Itoa(config.SharedConfig.ServerPort))

	logger.Info("Starting Server...")
	logger.Info("Go to http://127.0.0.1" + port + "/")

	if config.SharedConfig.ReusePort {
		// Start up the server
		listener, err := reuseport.Listen("tcp4", port)

		if err == nil {
			config := engine.Config{
				Address:  port,
				Listener: listener,
			}

			r.Run(fasthttp.WithConfig(config))
		} else {
			logger.Error("Could not listen on 127.0.0.1" + port)
		}
	} else {
		r.Run(fasthttp.New(port))
	}

}