亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
2. Timeouts in HTTP Clients
3. Channel Operations with time.After
4. Database Queries (e.g., with database/sql)
5. Graceful Shutdown with Timeout
Best Practices Summary
Home Backend Development Golang How to handle timeouts in Go applications?

How to handle timeouts in Go applications?

Jul 31, 2025 am 08:13 AM
go time out

Use context.WithTimeout to set controlled timeouts and always call cancel() to release resources. 2. Set explicit timeouts in HTTP clients using the Timeout field or configure granular timeouts with a custom Transport. 3. For channel operations, use select with time.After to avoid indefinite blocking, but prefer context or resettable timers in high-frequency scenarios to reduce memory overhead. 4. Apply context-aware methods like QueryContext and ExecContext for database queries to ensure timeout handling, provided the driver supports context. 5. Implement graceful shutdowns by calling server.Shutdown with a timeout-enabled context to allow active requests to complete. Always set timeouts on blocking operations, propagate context for cancellation, handle context.DeadlineExceeded explicitly, avoid time.After in loops, and test timeout behaviors to build reliable and resilient Go applications.

How to handle timeouts in Go applications?

Handling timeouts properly is crucial in Go applications, especially when dealing with network requests, database calls, or any operation that might hang indefinitely. Go provides robust tools—primarily through the context package and time package—to manage timeouts effectively.

How to handle timeouts in Go applications?

Here’s how to handle timeouts in different common scenarios:


1. Use context.WithTimeout for Controlled Timeouts

The most idiomatic way to handle timeouts in Go is using context.Context with a timeout. This allows you to cancel long-running operations and prevent resource leaks.

How to handle timeouts in Go applications?
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

result, err := doSomething(ctx)
if err != nil {
    if err == context.DeadlineExceeded {
        log.Println("operation timed out")
    } else {
        log.Printf("operation failed: %v", err)
    }
}

Key points:

  • Always call cancel() to release associated resources.
  • Check for context.DeadlineExceeded to distinguish timeout errors.
  • Pass the context through function calls (especially I/O-bound ones).

2. Timeouts in HTTP Clients

By default, http.Client has no timeout, which can cause your app to hang. Always set timeouts explicitly.

How to handle timeouts in Go applications?
client := &http.Client{
    Timeout: 10 * time.Second, // total timeout for the request
}

resp, err := client.Get("https://api.example.com/data")
if err != nil {
    if err, ok := err.(net.Error); ok && err.Timeout() {
        log.Println("HTTP request timed out")
    } else {
        log.Printf("HTTP error: %v", err)
    }
}

For more granular control (e.g., separate timeouts for dialing, TLS handshake, etc.), use a custom Transport:

client := &http.Client{
    Transport: &http.Transport{
        DialContext: (&net.Dialer{
            Timeout:   2 * time.Second,
            KeepAlive: 30 * time.Second,
        }).DialContext,
        TLSHandshakeTimeout:   3 * time.Second,
        ResponseHeaderTimeout: 5 * time.Second,
        IdleConnTimeout:       60 * time.Second,
    },
    Timeout: 15 * time.Second,
}

3. Channel Operations with time.After

When dealing with channels, use select with time.After to avoid blocking forever.

select {
case msg := <-ch:
    fmt.Println("Received:", msg)
case <-time.After(3 * time.Second):
    fmt.Println("Timed out waiting for message")
}

This is useful in message queues, worker pools, or inter-goroutine communication.

?? Note: time.After creates a timer even if the channel receives quickly. For frequent operations, prefer context.WithTimeout or pre-allocated timers to avoid memory overhead.


4. Database Queries (e.g., with database/sql)

Database queries should also be timeout-aware. Use context-aware methods like QueryContext, ExecContext, etc.

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

rows, err := db.QueryContext(ctx, "SELECT * FROM users WHERE active = ?", true)
if err != nil {
    if err == context.DeadlineExceeded {
        log.Println("DB query timed out")
    } else {
        log.Printf("DB error: %v", err)
    }
}

Ensure your driver supports context (most modern ones like pq, mysql, pgx do).


5. Graceful Shutdown with Timeout

When shutting down a server, use timeouts to allow in-flight requests to complete.

srv := &http.Server{Addr: ":8080"}

// Graceful shutdown on signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
    <-c
    ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
    defer cancel()
    srv.Shutdown(ctx)
}()

log.Fatal(srv.ListenAndServe())

This ensures the server stops gracefully without killing active connections abruptly.


Best Practices Summary

  • ? Always set timeouts on network operations.
  • ? Use context for propagation and cancellation.
  • ? Prefer context.WithTimeout over time.After in complex flows.
  • ? Handle context.DeadlineExceeded explicitly.
  • ? Avoid using time.After in tight loops—use time.NewTimer or reset timers.
  • ? Test timeout paths (e.g., using mocked slow services).

Timeouts aren’t just about reliability—they’re about building predictable, resilient systems. In Go, combining context, time, and proper error handling gives you fine-grained control with minimal overhead.

Basically, if it can block, it should have a timeout.

The above is the detailed content of How to handle timeouts in Go applications?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does the switch statement work in Go? How does the switch statement work in Go? Jul 30, 2025 am 05:11 AM

Go's switch statement will not be executed throughout the process by default and will automatically exit after matching the first condition. 1. Switch starts with a keyword and can carry one or no value; 2. Case matches from top to bottom in order, only the first match is run; 3. Multiple conditions can be listed by commas to match the same case; 4. There is no need to manually add break, but can be forced through; 5.default is used for unmatched cases, usually placed at the end.

How to use reflection in Go? How to use reflection in Go? Jul 28, 2025 am 12:26 AM

Usereflect.ValueOfandreflect.TypeOftogetruntimevaluesandtypes;2.Inspecttypedetailswithreflect.TypemethodslikeName()andKind();3.Modifyvaluesviareflect.Value.Elem()andCanSet()afterpassingapointer;4.CallmethodsdynamicallyusingMethodByName()andCall();5.R

how to break from a nested loop in go how to break from a nested loop in go Jul 29, 2025 am 01:58 AM

In Go, to break out of nested loops, you should use labeled break statements or return through functions; 1. Use labeled break: Place the tag before the outer loop, such as OuterLoop:for{...}, use breakOuterLoop in the inner loop to directly exit the outer loop; 2. Put the nested loop into the function, and return in advance when the conditions are met, thereby terminating all loops; 3. Avoid using flag variables or goto, the former is lengthy and easy to make mistakes, and the latter is not recommended; the correct way is that the tag must be before the loop rather than after it, which is the idiomatic way to break out of multi-layer loops in Go.

How to handle timeouts in Go? How to handle timeouts in Go? Jul 27, 2025 am 03:44 AM

Usecontext.WithTimeouttocreateacancellablecontextwithadeadlineandalwayscallcancel()toreleaseresources.2.ForHTTPrequests,settimeoutsusinghttp.Client.Timeoutorusecontextviahttp.NewRequestWithContextforper-requestcontrol.3.Ingoroutineswithchannels,usese

Using the Context Package in Go for Cancellation and Timeouts Using the Context Package in Go for Cancellation and Timeouts Jul 29, 2025 am 04:08 AM

Usecontexttopropagatecancellationanddeadlinesacrossgoroutines,enablingcooperativecancellationinHTTPservers,backgroundtasks,andchainedcalls.2.Withcontext.WithCancel(),createacancellablecontextandcallcancel()tosignaltermination,alwaysdeferringcancel()t

Efficient JSON Parsing and Manipulation in Go Efficient JSON Parsing and Manipulation in Go Jul 27, 2025 am 03:55 AM

UsestructswithPERJSontagsFeRpredictabledatoensurefast, safeparsingwithcompile-timetypesafety.2.avoidmap [string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] interface {string] }duetoreflectionoverheadandruntimetypeassertionsunlessdealingwithtrulydynamicJSON.3.Usejson.RawMessagefordeferredorselectivep

Building a GraphQL Server in Go Building a GraphQL Server in Go Jul 28, 2025 am 02:10 AM

InitializeaGomodulewithgomodinit,2.InstallgqlgenCLI,3.Defineaschemainschema.graphqls,4.Rungqlgeninittogeneratemodelsandresolvers,5.Implementresolverfunctionsforqueriesandmutations,6.SetupanHTTPserverusingthegeneratedschema,and7.RuntheservertoaccessGr

Performance benefits of switching to Go Performance benefits of switching to Go Jul 28, 2025 am 01:53 AM

Gooffersfasterexecutionspeedduetocompilationtonativemachinecode,outperforminginterpretedlanguageslikePythonintaskssuchasservingHTTPrequests.2.Itsefficientconcurrencymodelusinglightweightgoroutinesenablesthousandsofconcurrentoperationswithlowmemoryand

See all articles