其他分享
首页 > 其他分享> > 记录下并发超发的问题

记录下并发超发的问题

作者:互联网

      //简单判断库存
        $users = Users::query()->where('id', 1)->first();
        if ($users['count'] <= 0) {
            Log::info("库存不足");
            return "库存不足";
        }
        Users::query()->where('id', 1)->decrement('count', 1);
package main

import (
	"net/http"
	"sync"
)

func main() {
	wait := sync.WaitGroup{} //并发锁
	for i := 0; i < 180; i++ {
		wait.Add(1) //启动一个协程登记+1
		go func(w *sync.WaitGroup) {
			defer w.Done()
			resp, _ := http.Get("http://www.laraval8.io/lock")
			defer resp.Body.Close()
		}(&wait)
	}
	wait.Wait()
}

标签:defer,http,记录,sync,并发,WaitGroup,超发,wait
来源: https://www.cnblogs.com/tanbinghao/p/15164999.html