Skip to content

Latest commit

 

History

History

01_cancellation

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

🚦 Cancel Pattern

🌟 Introduction

The Cancel Pattern uses a signaling channel (commonly named done) to:

  1. Notify goroutines when they should stop.
  2. Allow for safe and controlled termination of goroutines.

This pattern ensures that goroutines do not run indefinitely, preventing potential resource leaks or unwanted side effects.

🚀 Example Code

package main

import (
	"fmt"
	"time"
)

func main() {
	// Function that performs work until the 'done' channel is closed
	doWork := func(done <-chan interface{}, strings <-chan string) <-chan interface{} {
		terminated := make(chan interface{})
		go func() {
			defer fmt.Println("doWork exited.")
			defer close(terminated)
			for {
				select {
				case s := <-strings:
					fmt.Println(s)
				case <-done:
					// Exit when 'done' is closed or receives a signal
					return
				}
			}
		}()
		return terminated
	}

	done := make(chan interface{})
	terminated := doWork(done, nil)

	go func() {
		// Stop the doWork goroutine after 1 second
		time.Sleep(1 * time.Second)
		fmt.Println("Canceling doWork goroutine...")
		close(done)
	}()

	<-terminated
	fmt.Println("Done.")
}

🔍 Explanation

1. Key Components

  • done Channel:

    • Acts as a signal to stop the goroutine.
    • Closing the channel (close(done)) tells the goroutine to exit.
  • strings Channel:

    • Represents a stream of data (in this case, string values) for the goroutine to process.
  • terminated Channel:

    • Used to signal the caller (main function) that the goroutine has exited.

2. How It Works

  • The doWork function:

    • Starts a goroutine that listens to the done and strings channels using a select statement.
    • If the done channel is closed, the goroutine safely exits.
  • The main function:

    • Closes the done channel after 1 second, signaling the goroutine to terminate.

💡 Output

When running the code, the output will look like this:

Canceling doWork goroutine...
doWork exited.
Done.

🛠️ Key Features of Cancel Pattern

  1. Safe Goroutine Termination:

    • Prevents uncontrolled goroutine execution, which can lead to memory leaks or excessive resource consumption.
  2. Simple and Effective:

    • Using a done channel is straightforward and works well for basic use cases.
  3. Decoupled Signal Handling:

    • The goroutine does not depend on specific implementation details; it just listens for a stop signal.