timeout

command
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

README

Timeout middleware

In this example, we will try to create mux server with timeout middleware enabled.

Table of Contents generated with DocToc

Quick start

Get rk-mux package from the remote repository.

go get -u github.com/rookie-ninja/rk-mux
Code

Add rkmuxtimeout.Interceptor() with option.

import     "github.com/rookie-ninja/rk-mux/interceptor/timeout"
    // ********************************************
    // ********** Enable interceptors *************
    // ********************************************
	interceptors := []mux.MiddlewareFunc{
        rkmuxtimeout.Interceptor(),
    }

Options

Name Default Description
WithEntryNameAndType(entryName, entryType string) entryName=mux, entryType=mux entryName and entryType will be used to distinguish options if there are multiple interceptors in single process.
WithTimeoutAndResp(time.Duration, http.HandlerFunc) 5*time.Second, response with http.StatusRequestTimeout Set timeout interceptor with all routes.
WithTimeoutAndRespByPath(path string, time.Duration, http.HandlerFunc) "", 5*time.Second, response with http.StatusRequestTimeout Set timeout interceptor with specified path.
	// ********************************************
	// ********** Enable interceptors *************
	// ********************************************
	interceptors := []mux.MiddlewareFunc{
		rkmuxtimeout.Interceptor(
		// Entry name and entry type will be used for distinguishing interceptors. Recommended.
		rkmuxtimeout.WithEntryNameAndType("greeter", "mux"),
		//
		// Provide timeout and response handler, a default one would be assigned with http.StatusRequestTimeout
		// This option impact all routes
		//rkmuxtimeout.WithTimeoutAndResp(time.Second, nil),
		//
		// Provide timeout and response handler by path, a default one would be assigned with http.StatusRequestTimeout
		//rkmuxtimeout.WithTimeoutAndRespByPath("/rk/v1/healthy", time.Second, nil),
		),
	}
Context Usage
Name Functionality
rkmuxctx.GetLogger(req, writer) Get logger generated by log interceptor. If there are X-Request-Id or X-Trace-Id as headers in incoming and outgoing metadata, then loggers will has requestId and traceId attached by default.
rkmuxctx.GetEvent(req) Get event generated by log interceptor. Event would be printed as soon as RPC finished.
rkmuxctx.GetIncomingHeaders(req) Get incoming header.
rkmuxctx.AddHeaderToClient(writer, "k", "v") Add k/v to headers which would be sent to client. This is append operation.
rkmuxctx.SetHeaderToClient(writer, "k", "v") Set k/v to headers which would be sent to client.
rkmuxctx.GetJwtToken(req) Get jwt token if exists
rkmuxctx.GetCsrfToken(req) Get csrf token if exists

Example

In this example, we enable log and panic interceptor either to monitor RPC status.

Start server
$ go run greeter-server.go
Output
  • Response
$ curl "localhost:8080/v1/greeter?name=rk-dev"
{
    "error":{
        "code":408,
        "status":"Request Timeout",
        "message":"Request timed out!",
        "details":[]
    }
}
  • Server side (zap & event)
2021-12-30T02:58:09.948+0800    INFO    timeout/greeter-server.go:93    Received request from client.
------------------------------------------------------------------------
endTime=2021-12-30T02:58:10.953412+08:00
startTime=2021-12-30T02:58:09.948238+08:00
elapsedNano=1005173310
timezone=CST
ids={"eventId":"d652d467-b5ea-4832-9a14-c50141955b36"}
app={"appName":"rk","appVersion":"","entryName":"mux","entryType":"mux"}
env={"arch":"amd64","az":"*","domain":"*","hostname":"lark.local","localIP":"192.168.101.5","os":"darwin","realm":"*","region":"*"}
payloads={"apiMethod":"GET","apiPath":"/v1/greeter","apiProtocol":"HTTP/1.1","apiQuery":"name=rk-dev","userAgent":"curl/7.64.1"}
error={}
counters={"timeout":1}
pairs={}
timing={}
remoteAddr=localhost:54965
operation=/v1/greeter
resCode=408
eventStatus=Ended
EOE
Code

Documentation

Overview

Copyright (c) 2021 rookie-ninja

Use of this source code is governed by an Apache-style license that can be found in the LICENSE file.

Jump to

Keyboard shortcuts

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