Giter VIP home page Giter VIP logo

gin-inject's Issues

panic on concurrent condition

问题

这个项目的依赖容器基于go-macaron/inject,内部实现是map

type injector struct {
	values map[reflect.Type]reflect.Value
	parent Injector
}

依赖注入调用的是injectorMap方法

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.Contextnil,估计还是并发写的锅

修复

因为go-macaron/inject实际只有一个文件,能不能复制出来采用sync.Map实现

Make Gin Great Again!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.