Home > Article > Backend Development > Golang high concurrency code sharing
Today, the leader asked why Golang was used. My colleague replied that the syntax is simple, the language is new, and it supports high concurrency. How to achieve high concurrency? The following article mainly introduces you to the relevant information on how to use Golang to write high concurrency code. The article introduces it in detail through example code. Friends who need it can refer to it. Let’s take a look. Bar.
Preface
I have always been confused about how Golang handles high concurrent http requests. I have also checked many related blogs in the past few days. I seem to understand, but I don’t know how to write the specific code
In the afternoon, I accidentally saw an article by a foreign technician on the Developer Toutiao APP, using Golang to handle millions of requests per minute. After reading the article I wrote the code myself and wrote down my experience below
Key points
Put the request into the queue , a worker pool (pool) is formed by a certain number (such as the number of CPU cores) goroutine, and the workers in the worker pool read the queue and execute tasks
Instance code
The following code has been simplified by the author based on his own understanding, mainly to express his personal ideas. In actual back-end development, it is modified according to the actual scenario
func doTask() { //耗时炒作(模拟) time.Sleep(200 * time.Millisecond) wg.Done() } //这里模拟的http接口,每次请求抽象为一个job func handle() { //wg.Add(1) job := Job{} JobQueue <- job } var ( MaxWorker = 1000 MaxQueue = 200000 wg sync.WaitGroup ) type Worker struct { quit chan bool } func NewWorker() Worker { return Worker{ quit: make(chan bool)} } // Start method starts the run loop for the worker, listening for a quit channel in // case we need to stop it func (w Worker) Start() { go func() { for { select { case <-JobQueue: // we have received a work request. doTask() case <-w.quit: // we have received a signal to stop return } } }() } // Stop signals the worker to stop listening for work requests. func (w Worker) Stop() { go func() { w.quit <- true }() } type Job struct { } var JobQueue chan Job = make(chan Job, MaxQueue) type Dispatcher struct { } func NewDispatcher() *Dispatcher { return &Dispatcher{} } func (d *Dispatcher) Run() { // starting n number of workers for i := 0; i < MaxWorker; i++ { worker := NewWorker() worker.Start() } }
Test
func Benchmark_handle(b *testing.B) { runtime.GOMAXPROCS(runtime.NumCPU()) d := NewDispatcher() d.Run() for i:=0;i<10000;i++ { wg.Add(1) handle() } wg.Wait() }
Test results
pkg: golang-study-demo/goroutine 1 2029931652 ns/op PASS
1w tasks are placed in the queue, 1000 workers execute the tasks, each task takes 200ms, and it takes 200ms to complete the task execution More than 2s
The above is just the author's personal opinion. I don't know if my understanding of Golang concurrent programming is correct. There are mistakes. I hope an expert can give me some guidance. Thank you
The above is the detailed content of Golang high concurrency code sharing. For more information, please follow other related articles on the PHP Chinese website!