- laravel代码
//简单判断库存
$users = Users::query()->where(‘id‘, 1)->first();
if ($users[‘count‘] <= 0) {
Log::info("库存不足");
return "库存不足";
}
Users::query()->where(‘id‘, 1)->decrement(‘count‘, 1);
- go协程并发测试
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()
}