timeout

package
v0.0.0-...-a0b1be5 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: MIT, 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 represents a pool of buffers.

func (*BufferPool) Get

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

Get returns a buffer from the buffer pool. If the pool is empty, a new buffer is created and returned.

func (*BufferPool) Put

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

Put adds a buffer back to the pool.

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 override Status func here, or the http status code returned by gin.Context.Writer.Status() will always be 200 in other custom gin middlewares.

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 sends an HTTP response header with the provided status code. If the response writer has already written headers or if a timeout has occurred, this method does nothing.

func (*Writer) WriteString

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

WriteString will write string to response body

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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