今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Worker pools
- [Instructor] So far, the Orders app uses channels to synchronize and pass information to goroutines. In this section, we will be looking at how to use them to process multiple results concurrently. We will be implementing the new stats endpoint, which allows us to view order statistics. We now have a new StatsService, which is called by the repo to calculate stats on processed orders. The repo communicates with the StatsService using a channel, sending it orders as they are processed. The StatsService uses a new results struct, which contains a Mutex to ensure no interleaving reads and writes happen on it. Let's have a look at the code. We have created a new statistic model. This model will be returned by the handlers. It contains fields for completed orders, rejected orders and the revenue of the app on lines five to nine. Moving on to the result. The result struct is a thread-safe wrapper around the statistics…