golang实现协程安全的几种方式

版本 golang – 1.12.4 golang协程同步 1.channel - monitor goroutine var deposits = make(chan int) // send amount to deposit var balances = make(chan int) // receive balance func Deposit(amount int) { deposits <- amount } func Balance() int { return <-balances } func teller() { var balance int // balance is confined to teller goroutine for { select { case amount := <-deposits: balance += amount case balances <- balance: } } } func init() { go teller() // start the monitor goroutine } 2.channel - serial confinement ...

May 13, 2019

用golang实现mongodb数据库连接池-高级篇-协程安全

版本 golang – 1.12.4 mongodb – 4.0 go driver – 1.0.0 简介 在上一篇《用golang实现mongodb数据库连接池-基本篇》我们实现了mongodb的golang driver按序使用的基本版,但还需要进一步提升效率和高并发安全。本篇张实现高效率协程安全版。 data race 什么是data race,考虑如下代码: var balance int func Deposit(amount int){ balance = balance + amount} func Balance() int { return balance} //Alice: go func(){ bank.Deposit(200) // A1 fmt.Println("=", bank.Balance()) // A2 }() //Bob go bank.Deposit(100) // B 当alice和bob同时执行如上的操作,最后的存款有几种可能性? 根据直觉会有3种可能: alice first bob first alice/bob/alice 0 0 0 A1 200 B 100 A1 200 A2 “=200” A1 300 B 300 B 300 A2 “=300” A2 “=300” 这个结果最后存款都是剩余300似乎也没什么问题,但是这里还有第4种可能,那就是bob的存款操作发生在A1的balance + amount之后,但是在A1的balance =之前,那么会出现什么? ...

May 11, 2019

用golang实现mongodb数据库连接池-基本篇

版本 golang – 1.12.4 mongodb – 4.0 go driver – 1.0.0 简介 mongodb的数据库driver在官方文档里面明确写明所有的数据库连接需要自己建立和释放,而且建议尽量复用已有的建立,那么也就是说driver里面并未实现连接池的功能。在我们实际应用中就需要自己实现这套数据库连接池提升程序和数据库之间的执行效率。 设计思路 用一个数组来存放数据库连接的指针,并记录每一个指针两个状态: a.是否申请了数据库连接 b.这个连接是否已经给系统在使用中。举个例子就比较好理解了: 申请一个用于存放数据连接的数组,一开始空的什么都没有 程序需要一个数据库连接,连接池把数组第一个位置建立一个数据库连接,并把这个连接的状态置为:a.已申请 b.已给系统 程序使用完释放数据库连接,现在数据库指针状态为:a.已申请 b.未使用 程序需要新申请一个数据库连接,那么就回到了第2的状态。 核心代码 const( MAX_CONNECTION = 10 INITIAL_CONNECTION = 4 AVAILABLE = false USED = true ) /* 代码取了一个巧,用实际存放数据库指针的大小ClientPool.size和mongodata.flag来表示上述a,b两个状态 如果mongodata.flag都为USED,那么需要新申请个数据库连接: size++ clientList: the client pool clientAvailable: the available flag, means the location and available flag in the client pool size: the size of allocated client pool <= MAX_CONNECTION */ type mongodata struct{ client *mongo.Client pos int flag bool } type ClientPool struct{ clientList [MAX_CONNECTION]mongodata size int } //create a new database connection to the pool func (cp *ClientPool) allocateCToPool(pos int) (err error){ cp.clientList[pos].client, err = Dbconnect() if err != nil { utils.Logger.SetPrefix("WARNING ") utils.Logger.Println("allocateCToPool - allocateCToPool failed,position: ", pos, err) return err } cp.clientList[pos].flag = USED cp.clientList[pos].pos = pos return nil } //apply a connection from the pool func (cp *ClientPool) getCToPool(pos int){ cp.clientList[pos].flag = USED } //free a connection back to the pool func (cp *ClientPool) putCBackPool(pos int){ cp.clientList[pos].flag = AVAILABLE } //program apply a database connection func GetClient() (mongoclient *mongodata, err error) { for i:=1; i<cp.size; i++ { if cp.clientList[i].flag == AVAILABLE{ return &cp.clientList[i], nil } } if cp.size < MAX_CONNECTION{ err = cp.allocateCToPool(cp.size) if err != nil { utils.Logger.SetPrefix("WARNING ") utils.Logger.Println("GetClient - DB pooling allocate failed", err) return nil, err } pos := cp.size cp.size++ return &cp.clientList[pos], nil } else { utils.Logger.SetPrefix("WARNING ") utils.Logger.Println("GetClient - DB pooling is fulled") return nil, errors.New("DB pooling is fulled") } } //program release a connection func ReleaseClient(mongoclient *mongodata){ cp.putCBackPool(mongoclient.pos) } 以上就是核心代码实现,但是这个代码有一个问题,就是在高并发下并非协程安全,这个留在下一篇《用golang实现mongodb数据库连接池-高级篇-协程安全》来优化。 ...

May 10, 2019

about quality

The company invited me to do a training crouse about the software’s quality, it’s an old topic every project book talk about it, every quality master discuss it, the cmmi and agile process have such a huge practices about it, what should i do about it? maybe i just list what i thought is important for the quality and what’s wrong with our modern software development process. the passion i think most of us going to the industry is because we love this, we love the feel of change something through the finger.if you have this feeling, then you are good damn of it ...

May 16, 2018