当前位置:网站首页>[golang] follow the object pool sync Pool
[golang] follow the object pool sync Pool
2022-06-23 20:05:00 【DDGarfield】
Learn some skills in the source code
1. from Run() Start
stay go Linguistic gin In the frame , adopt .Run() start-up web service . Let's look at the source code :
//gin.go
func (engine *Engine) Run(addr ...string) (err error) {
defer func() { debugPrintError(err) }()
address := resolveAddress(addr)
debugPrint("Listening and serving HTTP on %s\n", address)
err = http.ListenAndServe(address, engine)
return
}
among ListenAndServe yes net/http The listening address and processor specified in the library
//net/http/server.go
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
}
You can see gin What is called in the source code is engine *Engine As Handler Parameters , Continue to check Handler Source code :
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
indeed Handler It's an interface , Interface method :ServeHTTP(ResponseWriter, *Request)
that Engine It must have come true **ServeHTTP** Method
adopt VSCode Navigation , I did Engine The struct implements this method :
// 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)
}
2.sync.Pool On stage
Let's look at the third line of code in the previous section :
c := engine.pool.Get().(*Context)
type Engine struct {
//omit code
pool sync.Pool
}
among pool The fields are sync.Pool type , What is that sync.Pool?
original : This is a go The concept of a typical object pool in a language , In order to reduce the GC, Reduce the frequency of memory requests , Construct a pool of reusable objects ,engine.pool.Get() Is to take an object out of the pool , Cast to context The pointer .
- Through the object pool , Reduce the consumption of memory application and garbage collection for each temporary object creation
- Frequent object application and recycling , You can use object pooling to optimize your code
- It can avoid object reference or other interference , Without affecting the actual function of the code , After an object is removed from the pool , Then do initialization
3. Usage method
sync.Pool It's very simple to use :
3.1 Statement
//gin.go
func New() *Engine {
debugPrintWARNINGNew()
engine := &Engine{
RouterGroup: RouterGroup{
Handlers: nil,
basePath: "/",
root: true,
},
FuncMap: template.FuncMap{},
RedirectTrailingSlash: true,
RedirectFixedPath: false,
HandleMethodNotAllowed: false,
ForwardedByClientIP: true,
AppEngine: defaultAppEngine,
UseRawPath: false,
RemoveExtraSlash: false,
UnescapePathValues: true,
MaxMultipartMemory: defaultMultipartMemory,
trees: make(methodTrees, 0, 9),
delims: render.Delims{Left: "{{", Right: "}}"},
secureJsonPrefix: "while(1);",
}
engine.RouterGroup.engine = engine
// Realization New function
engine.pool.New = func() interface{} {
return engine.allocateContext()
}
return engine
}
- Just implement New Function . When there are no objects in the object pool , Will call
NewFunction creation .
3.2 Get()
obtain
Get()Used to get objects from the object pool , Because the return value isinterface{}, Therefore, type conversion is required , The above code has been shown .
c := engine.pool.Get().(*Context)
3.3 Put()
Put back
Put()After the object is used , Return object pool .
processed http After the request , Also put context Put back into the object pool :
engine.handleHTTPRequest(c)
engine.pool.Put(c)
3. matters needing attention
sync.Pool It's scalable , Concurrent security . Its size is limited only by the size of memory , It can be seen as a container for storing the values of reusable objects . It is designed to Store allocated but temporarily unused objects , When you need it, you can get it directly from pool To take . Values in any store can be deleted at any time without notice , It can be dynamically expanded under high load , When inactive, the object pool shrinks .
Because of the bold font above , So the object pool is more suitable for storing some temporary state independent data , Because the values stored in the object pool may be deleted during garbage collection .http Requested context Context is such a type .
4. Add another technique
stay gin The source code defines Engine There is a sentence at the bottom of the structure :
var _ IRouter = &Engine{}
An obscure anonymous variable : Anonymous variables don't take up memory space , Memory will not be allocated , What's the use of it ? It can't be that the author forgot , Such a famous open source library .
original go Languages often encounter a scenario , After writing a structure , This structure implements many interfaces , The problem is , After the code is complicated , Who can still remember which interface is implemented or not , What do I do ?
- Manual inspection , Um. , It's a way ,
Ctrl+Fcomparison , Find the method receiver , Um. , It's also a way
however , A better way , It depends on the compiler :
Define an anonymous variable var _ Interface type = Structure type pointer , This is mainly to ensure that the structure does implement the interface , The purpose is to Expose the problem in the compilation phase , Many third-party libraries and standard libraries do this , Worth learning .
Reference link
https://www.cnblogs.com/sunsky303/p/9706210.html
https://geektutu.com/post/hpg-sync-pool.html
------------------- End -------------------
边栏推荐
- 科班出身,结果外包都不要
- What are the useful personnel management software? Personnel management system software ranking!
- I came from a major, so I didn't want to outsource
- 5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
- 教你如何用网页开发APP
- 怎么开户?在国海证券开户安全吗?需要带什么?
- Importance and purpose of test
- SQL联合查询(内联、左联、右联、全联)的语法
- 墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
- 基于SSM实现微博系统
猜你喜欢

官宣.NET 7 预览版5

墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库

Application de JDBC dans les essais de performance

UST 崩盘后,稳定币市场格局将迎来新机遇?

Why is only one value displayed on your data graph?

ElastricSearch第二弹之分片原理

Activity registration | introduction to mongodb 5.0 sequential storage features

RStudio 1.4软件安装包和安装教程

Kubernetes 资源拓扑感知调度优化

Importance and purpose of test
随机推荐
【Golang】深究字符串——从byte rune string到Unicode与UTF-8
SAP实施项目上的内部顾问与外部顾问,相互为难还是相互成就?
GaussDB(DWS) 数据库智能监控运维服务-节点监控指标
Application de JDBC dans les essais de performance
Goldfish rhca memoirs: do447 managing user and team access -- effectively managing users with teams
Idea console displays Chinese garbled code
Development notes of wedding studio applet based on wechat applet
Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions
QGIS import WMS or WMTs
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
LeetCode 1079. 活字印刷
打新债 要求 打新债安全吗
Syntax of SQL union query (inline, left, right, and full)
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
20 provinces and cities announce the road map of the meta universe
Ugeek's theory 𞓜 application and design of observable hyperfusion storage system
Helix QAC is updated to 2022.1 and will continue to provide high standard compliance coverage
小程序开发框架推荐
【Golang】快速复习指南QuickReview(八)——goroutine
UST 崩盘后,稳定币市场格局将迎来新机遇?