timeout

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2023 License: MIT Imports: 6 Imported by: 0

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),
  ))
custom middleware
package main

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

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

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

func timeoutMiddleware() gin.HandlerFunc {
  return timeout.New(
    timeout.WithTimeout(500*time.Millisecond),
    timeout.WithHandler(func(c *gin.Context) {
      c.Next()
    }),
    timeout.WithResponse(testResponse),
  )
}

func main() {
  r := gin.New()
  r.Use(timeoutMiddleware())
  r.GET("/slow", func(c *gin.Context) {
    time.Sleep(800 * time.Millisecond)
    c.Status(http.StatusOK)
  })
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

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

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

BufferPool is Pool of *bytes.Buffer

func (*BufferPool) Get

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

Get a bytes.Buffer pointer

func (*BufferPool) Put

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

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

Writer is a writer with memory buffer

func NewWriter

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

NewWriter will return a timeout.Writer pointer

func (*Writer) FreeBuffer

func (w *Writer) FreeBuffer()

FreeBuffer will release buffer pointer

func (*Writer) Header

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

Header will get response headers

func (*Writer) Status

func (w *Writer) Status() int

Status we must implement this func, or the status code will always be 200 when call gin.Context.Writer.Status() in our custom gin middleware.

func (*Writer) Write

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

Write will write data to response body

func (*Writer) WriteHeader

func (w *Writer) WriteHeader(code int)

WriteHeader will write http status code

func (*Writer) WriteHeaderNow

func (w *Writer) WriteHeaderNow()

func (*Writer) WriteString

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

WriteString will write string to response body

Directories

Path Synopsis
_example
example01 command

Jump to

Keyboard shortcuts

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