forker

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2022 License: Apache-2.0 Imports: 11 Imported by: 1

README

forker

Go Reference

Get high performance http requests with fork process (forker inspired prefork feature in web servers)

prefork is a solution which is also used by other webservers

A server instance is opened for each processor core and incoming requests are shared between these instances

In order to distribute the load more evenly and handle more requests per second, there is a master that starts by the user, which then starts child processes on the other processor cores based on configuration

memory between the different processes is not shared, beacuse goroutines are independent processes

alt text

how to install

go get -u github.com/Ja7ad/forker

forker benchmark

forker tested 500 concurrent with 200k http requests

alt text

oha -c 500 -n 200000 --latency-correction --disable-keepalive http://localhost:8080
Summary:
  Success rate:	1.0000
  Total:	24.4907 secs
  Slowest:	0.5000 secs
  Fastest:	0.0004 secs
  Average:	0.0610 secs
  Requests/sec:	8166.3762

  Total data:	2.10 MiB
  Size/request:	11 B
  Size/sec:	87.72 KiB

Response time histogram:
  0.000 [1]     |
  0.050 [82408] |■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.100 [98705] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.150 [16180] |■■■■■
  0.200 [2245]  |
  0.250 [361]   |
  0.300 [59]    |
  0.350 [33]    |
  0.400 [3]     |
  0.450 [1]     |
  0.500 [4]     |

Latency distribution:
  10% in 0.0291 secs
  25% in 0.0404 secs
  50% in 0.0557 secs
  75% in 0.0754 secs
  90% in 0.0989 secs
  95% in 0.1170 secs
  99% in 0.1581 secs

Details (average, fastest, slowest):
  DNS+dialup:	0.0259 secs, 0.0001 secs, 0.3010 secs
  DNS-lookup:	0.0000 secs, 0.0000 secs, 0.0239 secs

Example

http server
package main

import (
	"github.com/Ja7ad/forker"
	"log"
	"net/http"
)

func main() {
	srv := &http.Server{
		Handler: GreetingHandler(),
	}

	f := forker.New(srv)

	log.Fatalln(f.ListenAndServe(":8080"))

}

func GreetingHandler() http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("greeting!!!"))
	}
}
grpc server

benchmark

package main

import (
	"github.com/Ja7ad/forker"
	"google.golang.org/grpc/health"
	"google.golang.org/grpc/health/grpc_health_v1"
	"google.golang.org/grpc/reflection"
	"log"
)

func main() {
	f := forker.NewGrpcForker(nil)
	srv := f.GetGrpcServer()

	grpc_health_v1.RegisterHealthServer(srv, health.NewServer())

	reflection.Register(srv)

	log.Fatalln(f.ServeGrpc(":9090"))
}

echo framework

benchmark

package main

import (
	"github.com/Ja7ad/forker"
	"github.com/labstack/echo/v4"
)

func main() {
	f := forker.NewEchoForker()
	e := f.GetEcho()

	e.GET("/", Greeting)

	e.Logger.Fatal(f.Start(":8080"))
}

func Greeting(c echo.Context) error {
	return c.String(200, "greeting")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrReuseportOnWindows = errors.New("please enable reuseport for windows")
	ErrOverRecovery       = errors.New("exceeding recovery child of forker")
)

Functions

This section is empty.

Types

type EchoForker added in v1.1.0

type EchoForker interface {
	StartEcho(address string) error
	GetEcho() *echo.Echo
	// contains filtered or unexported methods
}

func NewEchoForker added in v1.1.0

func NewEchoForker(opts ...Option) EchoForker

NewEchoForker create for listen and serve echo

type Fork

type Fork struct {
	Network   Network // Network is net type tcp4, tcp, tcp6, udp, udp4, udp6
	ReusePort bool    // ReusePort use for windows support child process base on system call
	// contains filtered or unexported fields
}

func (*Fork) ChildPids

func (f *Fork) ChildPids() []int

ChildPids list child process PID

func (*Fork) GetEcho added in v1.1.0

func (f *Fork) GetEcho() *echo.Echo

GetEcho return echo object

func (*Fork) GetGrpcServer added in v1.2.0

func (f *Fork) GetGrpcServer() *grpc.Server

GetGrpcServer return grpc server object

func (*Fork) ListenAndServe

func (f *Fork) ListenAndServe(address string) error

ListenAndServe listen and serve http server

func (*Fork) ListenAndServeTLS

func (f *Fork) ListenAndServeTLS(address, certFile, keyFile string) error

ListenAndServeTLS listen and serve http server with tls support

func (*Fork) NumOfChild

func (f *Fork) NumOfChild() int

NumOfChild number of child process

func (*Fork) ServeGrpc added in v1.2.0

func (f *Fork) ServeGrpc(address string) error

ServeGrpc serve grpc server

func (*Fork) StartEcho added in v1.1.2

func (f *Fork) StartEcho(address string) error

StartEcho listener echo

type Forker

type Forker interface {
	ListenAndServe(address string) error
	ListenAndServeTLS(address, certFile, keyFile string) error
	// contains filtered or unexported methods
}

func New

func New(httpServer *http.Server, opts ...Option) Forker

New create forker for listen and serve http server

type GrpcForker added in v1.2.0

type GrpcForker interface {
	ServeGrpc(address string) error
	GetGrpcServer() *grpc.Server
	// contains filtered or unexported methods
}

func NewGrpcForker added in v1.2.0

func NewGrpcForker(grpcServerOpts []grpc.ServerOption, opts ...Option) GrpcForker

NewGrpcForker create for listen and serve grpc server

type Network

type Network int
const (
	TCP4 Network = iota + 1
	TCP6
	UDP
	UDP4
	UDP6
)

func (Network) String

func (i Network) String() string

type Option

type Option func(f *Fork)

func WithCustomNetwork

func WithCustomNetwork(network Network) Option

WithCustomNetwork set network type listing type

func WithReusePort

func WithReusePort(reusePort bool) Option

WithReusePort enable reuse port option for windows

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL