gate

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2019 License: MIT Imports: 1 Imported by: 0

README

gate

Build Status Go Report Card GoDoc License MIT

Limit the number of busy handlers (at the gate)

Installation

go get -u github.com/TV4/gate

Usage

package main

import (
	"net/http"
	"time"

	"github.com/TV4/gate"
)

func index(w http.ResponseWriter, r *http.Request) {
	time.Sleep(1 * time.Second)
	w.Write([]byte("Hello, World!\n"))
}

func main() {
	http.Handle("/", gate.HandlerFunc(index, 10))
	http.ListenAndServe(":2016", nil)
}
$ wrk http://localhost:2016/
Running 10s test @ http://localhost:2016/
  2 threads and 10 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.00s     1.41ms   1.01s    70.00%
    Req/Sec     4.20      0.41     5.00     80.00%
  100 requests in 10.07s, 12.79KB read
Requests/sec:      9.93
Transfer/sec:      1.27KB

License (MIT)

Copyright (c) 2016-2017 TV4

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package gate allows you to limit the number of busy handlers (at the gate)

Installation

Just go get the package:

go get -u github.com/TV4/gate

Usage

A small usage example

package main

import (
	"net/http"
	"time"

	"github.com/TV4/gate"
)

func index(w http.ResponseWriter, r *http.Request) {
	time.Sleep(1 * time.Second)
	w.Write([]byte("Hello, World!\n"))
}

func main() {
	http.Handle("/", gate.HandlerFunc(index, 10))
	http.ListenAndServe(":2016", nil)
}

Index

Constants

This section is empty.

Variables

View Source
var BlockedHandler = http.NotFoundHandler()

BlockedHandler is the handler that is used when n < 1

Functions

func Handler

func Handler(h http.Handler, limit int) http.Handler

Handler takes a http.Handler, limit number

func HandlerFunc

func HandlerFunc(h http.HandlerFunc, limit int) http.Handler

HandlerFunc takes a http.HandlerFunc and limit number

Types

type Gate added in v0.2.0

type Gate struct {
	// contains filtered or unexported fields
}

func New added in v0.2.0

func New(limit, waitLimit int) *Gate

func (*Gate) Handler added in v0.2.0

func (g *Gate) Handler(h http.Handler) http.Handler

func (*Gate) Waiting added in v0.2.0

func (g *Gate) Waiting() int

Waiting returns the number of requests waiting at the gate

Jump to

Keyboard shortcuts

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