wuhan005 / gin-inject Goto Github PK
View Code? Open in Web Editor NEWGin + 依赖注入的小尝试 / Gin with dependency injection. 【Make Gin Great Again!】
Gin + 依赖注入的小尝试 / Gin with dependency injection. 【Make Gin Great Again!】
这个项目的依赖容器基于go-macaron/inject,内部实现是map
type injector struct {
values map[reflect.Type]reflect.Value
parent Injector
}
依赖注入调用的是injector
的Map
方法
func (i *injector) Map(val interface{}) TypeMapper {
i.values[reflect.TypeOf(val)] = reflect.ValueOf(val)
return i
}
但是,Go的原生map
不支持并发写,所以在并发状态下,会报错
if h.flags&hashWriting != 0 { //位置:runtime/map.go line:585 on Go 1.15.4
throw("concurrent map writes")
}
尝试对写map的部分加锁
sMapLock.Lock()
inj.Map(c)
inj.Map(ginsession.FromContext(c))
for _, dep := range deps {
inj.Map(dep)
}
sMapLock.Unlock()
但是在handler
内部报错runtime error: invalid memory address or nil pointer dereference
问题是注入进去的gin.Context
为nil
,估计还是并发写的锅
因为go-macaron/inject实际只有一个文件,能不能复制出来采用sync.Map
实现
Make Gin Great Again!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.