Concurrency allows us to handle multiple tasks independently from each other. Goroutines are a simple way to process multiple tasks independently. In this post we progressively enhance a http handler which accepts files an explore various concurrency patterns in Go utilizing channels and the sync package.
Setup
Before getting into concurrency patterns let’s set the stage. Imagine we have a HTTP handler that accepts multiple files via form and processes the files in some way.
func processFile(file multipart.File) { // do something with the file fmt.Println("Processing file...") time.Sleep(100 * time.Millisecond) // Simulating file processing time } func UploadHandler(w http.ResponseWriter, r *http.Request) { // limit to 10mb if err := r.ParseMultipartForm(10 <p>In the example above we receive files from a form and process them sequentially. If 10 files are uploaded it would take 1 second to complete the process and send a response to the client. <br> When handling many files this can become a bottleneck, however with Go's concurrency support we can easily solve this issue.</p> <h2> Wait Groups </h2> <p>To solve this we can process files concurrently. To spawn a new goroutine we can prefix a function call with the go keyword e.g. go processFile(f). However as goroutines are non blocking the handler might return before the process is finished, leaving files possibly unprocessed or returning an incorrect state. To wait for the processing of all files we can utilize sync.WaitGroup.<br> A WaitGroup waits for a number of goroutines to finish, for each goroutine we spawn we additionally should increase the counter in the WaitGroup this can be done with the Add function. When a goroutine is finished Done should be called so the counter is decreased by one. Before returning from the function Wait should be called which is blocking until the counter of the WaitGroup is 0.<br> </p> <pre class="brush:php;toolbar:false">func UploadHandler(w http.ResponseWriter, r *http.Request) { if err := r.ParseMultipartForm(10 <p>Now for each uploaded file a new goroutine is spawned this could overwhelm the system. One solution is to limit the number of spawned goroutines.</p> <h2> Limiting Concurrency With A Semaphore </h2> <p>A Semaphore is just a variable we can use to control access to common resources by multiple threads or in this case goroutines.<br><br> In Go we can utilize buffered channels to implement a semaphore.</p> <h3> Channels </h3> <p>Before getting into the implementation let's look at what channels are and the difference between buffered and unbuffered channels. </p> <p>Channels are a pipe through which we can send and receive data to communicate safely between go routines.<br> Channels must be created with the make function.<br> </p><pre class="brush:php;toolbar:false"> func processFile(file multipart.File) { // do something with the file fmt.Println("Processing file...") time.Sleep(100 * time.Millisecond) // Simulating file processing time } func UploadHandler(w http.ResponseWriter, r *http.Request) { // limit to 10mb if err := r.ParseMultipartForm(10 <p>Channels have a special operator Having the operator point at the channel ch <br> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173404051364120.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Goroutines and Channels: Concurrency Patterns in Go"><br> The animation visualizes a producer sending the value 1 through an unbuffered channel and the consumer reading from the channel.</p> <p>If the producer can send events faster than the consumer can handle then we have the option to utilize a <strong>buffered channel</strong> to queue up multiple messages without blocking the producer until the buffer is full. At the same time the consumer can handle the messages at its own pace.<br> </p> <pre class="brush:php;toolbar:false">func UploadHandler(w http.ResponseWriter, r *http.Request) { if err := r.ParseMultipartForm(10 <p>In this example the producer can send up to two items without blocking. When the capacity of the buffer is reached the producer will block until the consumer handled at least one message. </p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173404051459967.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Goroutines and Channels: Concurrency Patterns in Go"></p> <p>Back to the initial problem we want to limit the amount of goroutines processing files concurrently. To do this we can utilize buffered channels.<br> </p> <pre class="brush:php;toolbar:false">ch := make(chan int)
In this example we added a buffered channel with a capacity of 5, this allows us to process 5 files concurrently and limit strain on the system.
But what if not all files are equal? We might can reliably predict that different file types or file size require more resources to process. In this case we can utilize a weighted semaphore.
Weighted Semaphore
Simply put with a weighted semaphore we can assign more resources to a single tasks. Go already provides an implementation for a weighted semaphore within the extend sync package.
ch := make(chan int, 2)
In this version we created a weighted semaphore with 5 slots, if only images are uploaded for example the process handles 5 images concurrently, however if a PDF is uploaded 2 slots are acquired, which would reduce amount of files which can be handled concurrently.
Conclusion
We explored a few concurrency patterns in Go, utilizing sync.WaitGroup and semaphores to control the number of concurrent tasks. However there are more tools available, we could utilize channels to create a worker pool, add timeouts or use fan in/out pattern.
Additionally error handling is an important aspect which was mostly left out for simplicity.
One way to handle errors would be utilized channels to aggregate errors and handle them after all goroutines are done.
Go also provides a errgroup.Group which is related to sync.WaitGroups but adds handling of tasks which return errors.
The package can be found in the extend sync package.
The above is the detailed content of Goroutines and Channels: Concurrency Patterns in Go. For more information, please follow other related articles on the PHP Chinese website!

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
