liveBook · Manning

Deadline

As explained in 100 Go Mistakes and How to Avoid Them

A deadline is a time limit after which an operation or computation should be stopped or treated as expired.

A deadline marks a point in time after which ongoing work should stop or be treated as expired. It may be expressed as a duration from now or as a specific time. In Go, a context carries that deadline to operations such as I/O requests and channel waits. section 8.6.1 section 8.6

A common mistake is performing channel sends or receives as unconditional blocking operations. If the deadline expires while they wait, the function cannot respond promptly. Select on both the channel operation and ctx.Done(), then return ctx.Err(). section 8.6.4

type publisher interface {
    Publish(ctx context.Context, position flight.Position) error
}

Accept a context at the API boundary so the caller can communicate a deadline or cancellation signal. section 8.6.1

Publishing the latest radar position

A publisher has four seconds to publish a flight position because consumers only want the latest position. Create the context with context.WithTimeout, defer cancel(), and pass the derived context to Publish. The deferred cancellation releases the context's internal goroutine when the surrounding function returns. section 8.6.1

type publishHandler struct {
    pub publisher
}
 
func (h publishHandler) publishPosition(position flight.Position) error {
    ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second) #1
    defer cancel()                                                          #2
    return h.pub.Publish(ctx, position)                                     #3
}

The complete timeout-based publishing shape combines a four-second deadline, deferred cleanup, and context propagation. section 8.6.1

func f(ctx context.Context) error {
    // ...
    select {              #1
    case <-ctx.Done():
        return ctx.Err()
    case ch1 <- struct{}{}:
    }
 
    select {              #2
    case <-ctx.Done():
        return ctx.Err()
    case v := <-ch2:
        // ...
    }
}

Use select around every potentially blocking channel send or receive so expiration interrupts the operation instead of leaving it waiting. section 8.6.4

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains durations, absolute times, context propagation, cleanup, Done, Err, and interruptible channel operations.
Examples
Uses four-second radar-position publication and context-aware channel send and receive examples.
What it assumes
Assumes familiarity with goroutines, channels, functions, and Go contexts while defining deadline behavior explicitly.
What you can do after
Apply it via Go contexts to bound operations, clean up derived contexts, and diagnose expiration with ctx.Err().
How it connects
Connects deadlines with context.Context, cancellation, channels, goroutines, I/O, time.Duration, and time.Time.

Also explained in

Compare how 3 books explain deadline →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.