hrq

package module
v0.0.0-...-544350c Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2022 License: MIT Imports: 21 Imported by: 0

README

http-request-queue=(hrq)

http-request-queue=(hrq) is a simple HTTP request queue for the web server.

Install

go get github.com/sjqzhang/hrq

Basic Setup

hrq.Conf.Workers = 100
hrq.Conf.MaxQueueSize = 1000

Basic Usage

package main

import (
	"github.com/sjqzhang/hrq"
	"net/http"
)

func main() {
	// define http request handler
	hrq.GET( "/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello world"))
	})
	// start http server
	hrq.ListenAndServe(":8080")

}

integration with gin

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/sjqzhang/hrq"
	"io/ioutil"
	"net/http"
)
func main() {

	gin.SetMode(gin.ReleaseMode)
	gin.DefaultWriter = ioutil.Discard
	router := gin.Default()
	router.Use(hrq.MiddlewareForGin())// 使用中间件以激活队列功能
	hrq.GET("/hello", func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello world"))
	})

	router.GET("/world", func(c *gin.Context) {
		c.String(200, "world, hello ")
	})
	hrq.ApplyToGin(router) // 将hrq的路由应用到gin中
	router.Run(":8080")
}

integration with beego


package main

import (
	"fmt"
	"github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
	"github.com/sjqzhang/hrq"
	"net/http"
)


func main() {
	hrq.Conf.Workers = 100
	hrq.Conf.MaxQueueSize = 1000
	hrq.GET("/hello", func(w http.ResponseWriter, req *http.Request) {
		fmt.Println(req.Context())
		w.Write([]byte("hello world, from hrq"))
	})
	web.BeeApp.Handlers.AddMethod("GET", "/hello2", func(ctx *context.Context) {
		ctx.WriteString("hello2 world, from beego")
	})
	hrq.InstallFilterChanForBeego()
	hrq.ApplyToBeego(web.BeeApp)
	web.Run()

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Conf = &Config{
	Workers:       runtime.NumCPU() * 10,
	MaxQueueSize:  runtime.NumCPU() * 100,
	MaxConnection: 10000,

	TimeoutQueue:     3000,
	TempDir:          "/tmp",
	EnableOverload:   true,
	PerRequestQueue:  runtime.NumCPU() * 10,
	PerRequestWorker: runtime.NumCPU() * 5,
	workerOptions:    make(map[string]workerOption),
}

Functions

func ApplyFromGin

func ApplyFromGin(ginEngine *gin.Engine)

func ApplyToBeego

func ApplyToBeego(server *web.HttpServer)

func ApplyToEcho

func ApplyToEcho(e *echo.Echo)

func ApplyToGin

func ApplyToGin(ginEngine *gin.Engine)

func DELETE

func DELETE(path string, handle http.HandlerFunc, options ...workerOption)

DELETE is a shortcut for router.Handle(http.MethodDelete, path, handle)

func GET

func GET(path string, handle http.HandlerFunc, options ...workerOption)

func GetStat

func GetStat() map[string]*stat

get stat

func HEAD(path string, handle http.HandlerFunc, options ...workerOption)

HEAD is a shortcut for router.Handle(http.MethodHead, path, handle)

func InstallFilterChanForBeego

func InstallFilterChanForBeego()

func ListenAndServe

func ListenAndServe(addr string) error

func MiddlewareForEcho

func MiddlewareForEcho() echo.MiddlewareFunc

func MiddlewareForGin

func MiddlewareForGin() gin.HandlerFunc

func New

func New(conf *Config) *hrq

func OPTIONS

func OPTIONS(path string, handle http.HandlerFunc, options ...workerOption)

OPTIONS is a shortcut for router.Handle(http.MethodOptions, path, handle)

func PATCH

func PATCH(path string, handle http.HandlerFunc, options ...workerOption)

PATCH is a shortcut for router.Handle(http.MethodPatch, path, handle)

func POST

func POST(path string, handle http.HandlerFunc, options ...workerOption)

POST is a shortcut for router.Handle(http.MethodPost, path, handle)

func PUT

func PUT(path string, handle http.HandlerFunc, options ...workerOption)

PUT is a shortcut for router.Handle(http.MethodPut, path, handle)

func Router

func Router() *httprouter.Router

func SetGlobalHrq

func SetGlobalHrq(h *hrq)

set global hrq

func WithWorkerOption

func WithWorkerOption(workerCount int, maxQueue int) workerOption

Types

type Config

type Config struct {
	// The number of goroutines that will be used to handle requests.
	// If <= 0, then the number of CPUs will be used.
	Workers int
	// The size of the queue that will be used to store requests.
	// If <= 0, then the default value will be used.
	MaxQueueSize int
	// TempDir is the directory to use for temporary files.
	TempDir string
	// TimeoutQueue is the maximum duration before timing out read of the request.
	// If TimeoutQueue is zero, no timeout is set.
	TimeoutQueue int64
	// TimeoutProcess is the maximum duration before timing out processing of the request.
	// If TimeoutProcess is zero, no timeout is set.
	TimeoutProcess int64

	PerRequestWorker int

	PerRequestQueue int

	// MaxConnection
	MaxConnection int
	// enable overload
	EnableOverload bool
	// contains filtered or unexported fields
}

func (*Config) SetWorkerOption

func (c *Config) SetWorkerOption(method string, path string, option workerOption) *Config

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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