timeout

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2022 License: MIT Imports: 6 Imported by: 83

README

Timeout

Run Tests codecov Go Report Card GoDoc

Timeout wraps a handler and aborts the process of the handler if the timeout is reached.

Example

package main

import (
  "log"
  "net/http"
  "time"

  "github.com/gin-contrib/timeout"
  "github.com/gin-gonic/gin"
)

func emptySuccessResponse(c *gin.Context) {
  time.Sleep(200 * time.Microsecond)
  c.String(http.StatusOK, "")
}

func main() {
  r := gin.New()

  r.GET("/", timeout.New(
    timeout.WithTimeout(100*time.Microsecond),
    timeout.WithHandler(emptySuccessResponse),
  ))

  // Listen and Server in 0.0.0.0:8080
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

custom error response

Add new error response func:

func testResponse(c *gin.Context) {
  c.String(http.StatusRequestTimeout, "test response")
}

Add WithResponse option.

  r.GET("/", timeout.New(
    timeout.WithTimeout(100*time.Microsecond),
    timeout.WithHandler(emptySuccessResponse),
    timeout.WithResponse(testResponse),
  ))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...Option) gin.HandlerFunc

New wraps a handler and aborts the process of the handler if the timeout is reached

Types

type BufferPool added in v0.0.2

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

BufferPool is Pool of *bytes.Buffer

func (*BufferPool) Get added in v0.0.2

func (p *BufferPool) Get() *bytes.Buffer

Get a bytes.Buffer pointer

func (*BufferPool) Put added in v0.0.2

func (p *BufferPool) Put(buf *bytes.Buffer)

Put a bytes.Buffer pointer to BufferPool

type Option

type Option func(*Timeout)

Option for timeout

func WithHandler

func WithHandler(h gin.HandlerFunc) Option

WithHandler add gin handler

func WithResponse

func WithResponse(h gin.HandlerFunc) Option

WithResponse add gin handler

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout set timeout

type Timeout

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

Timeout struct

type Writer added in v0.0.2

type Writer struct {
	gin.ResponseWriter
	// contains filtered or unexported fields
}

Writer is a writer with memory buffer

func NewWriter added in v0.0.2

func NewWriter(w gin.ResponseWriter, buf *bytes.Buffer) *Writer

NewWriter will return a timeout.Writer pointer

func (*Writer) FreeBuffer added in v0.0.2

func (w *Writer) FreeBuffer()

FreeBuffer will release buffer pointer

func (*Writer) Header added in v0.0.2

func (w *Writer) Header() http.Header

Header will get response headers

func (*Writer) Write added in v0.0.2

func (w *Writer) Write(data []byte) (int, error)

Write will write data to response body

func (*Writer) WriteHeader added in v0.0.2

func (w *Writer) WriteHeader(code int)

WriteHeader will write http status code

func (*Writer) WriteString added in v0.0.2

func (w *Writer) WriteString(s string) (int, error)

WriteString will write string to response body

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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