control

package
v0.0.0-...-a2093d6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: LGPL-2.1 Imports: 5 Imported by: 0

README

gin的连接建立到路由函数处理的路径流程

1、engine提供了serverhttp, 对于req、response之类的都是由net/http提供了,所以gin其实只是一个路由处理的包 2、context是从sync.Pool中获取到的然后直接把w、req赋值进去

// ServeHTTP conforms to the http.Handler interface.
func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	c := engine.pool.Get().(*Context)
	c.writermem.reset(w)
	c.Request = req
	c.reset()

	engine.handleHTTPRequest(c)

	engine.pool.Put(c)
}

go-kit的连接建立以及路径处理流程

应该是类似上面的,它的transport也是提供了ServeHTTP这样的话,也可以直接使用gin或者原生的net/http处理

从需要达到的功能出发,分析如何进行切入,如何达到最小化修改原来的controller问题

如果需要接入go-kit 那么也就是说gin的request以及response等只能选一个,那么为了最小修改肯定选gin的

测试

curl -XPOST -d '{"s": "Go-Kit"}' http://localhost:8080/uppercase

curl -XPOST -d '{"s": "Go-Kit"}' http://localhost:8080/count

限流代码

go get golang.org/x/time/rate

除了流量限制之外 还需要能够接入go-kit提供的其他功能,需要进行

熔断

服务调用的链路上,如果服务下游服务访问太慢导致上游服务大连堆积,那么则会造成连锁反应,引起服务链的崩溃,导致整个应用不可用

go-kit提供: gobreaker、handy、hystrix-go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GinEndpoint = func(c *gin.Context, handler gin.HandlerFunc) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		handler(c)
		return nil, nil
	}
}
View Source
var NilDecoder = func(c context.Context, rw http.ResponseWriter, i interface{}) error { return nil }
View Source
var NilEncoder = func(c context.Context, r *http.Request) (request interface{}, err error) { return nil, nil }
View Source
var NilEndpoint = func(ctx context.Context, request interface{}) (interface{}, error) {
	return nil, nil
}

Functions

func InstallAdapter

func InstallAdapter(handler gin.HandlerFunc, adapters ...IGoKitAdapter) gin.HandlerFunc

Types

type GoKitAdapter

type GoKitAdapter struct{}

type IGoKitAdapter

type IGoKitAdapter interface {
	AdapterInit(args map[string]interface{}) error // 初始化
	Middleware() endpoint.Middleware               // gokit的中间件,配置监控、限流等功能
	// GinMiddleware() gin.HandlerFunc              // 使用于gin的中间件
	HandlerFunc(gin.HandlerFunc) gin.HandlerFunc // 用于包装gin的handlerfunc
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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