Home Backend Development Golang Learn more about the usage of Go Context

Learn more about the usage of Go Context

Apr 05, 2023 am 09:10 AM

Context in the Go language is very easy to use. Almost all Go programs use it to pass values ​​in the request range. It is a lightweight object that allows request-scoped values ​​to be passed across API boundaries, including cancellation signals, deadlines, request IDs, etc.

In this article, we will take an in-depth look at the usage of Go Context, understand its advantages and how to use it to improve the performance and robustness of your application.

What is Go Context?

Go Context is a standard library in the Go language, used to manage request range values. It provides applications with a lightweight and transitive method for passing requested variables between goroutines. It is mainly used to pass cancellation requests, timeout limits, tracking logs, request context, request data, etc.

Unlike Context in other programming languages, Go Context has some very special properties:

  • It is thread-safe.
  • Its value can be passed across goroutines.
  • It can be canceled or timed out to prevent long-running or unstoppable operations.
  • It can also be used to separate request cancellation and actual operation cancellation.

Usage scenarios

Go Context is a very versatile tool that can be used for applications in different scenarios, some of which include:

  1. Web Application

Web application is one of the most common usage scenarios. It can easily manage the context and request-specific metadata required to handle HTTP requests. During HTTP request processing, the Context is used to pass request IDs, request timeouts, cancellation signals, etc. across handlers. For example, Context is used to track session state when handling Websocket connections.

  1. Background service application

Background service application may need to traverse multiple APIs to provide data to external systems. Context is used to complete goroutine in such applications. More efficient termination, the best choice for this kind of application. You can use the Context WithCancel standard function to achieve this. If all goroutines use this Context, only one callback is needed to stop all subscriptions and clean up resources.

  1. Database and File Operations

Go Context is an ideal tool for handling large file and DB operations, as these operations can be resource and I/O intensive. In this case, Context is used to cancel the operation to avoid mistakes and runtime errors.

  1. Distributed Applications

In microservice architecture, Context is widely used to pass request-scoped information from the API to the call chain of each service. In this case, the trace ID is stored in the Context and passed when connecting to multiple services. This makes tracing the entire request line easy.

Usage of Context

Now that we have understood the basics of Go Context, we will explore how to use it to manage request-scoped values.

  1. Create a Context

In the Go language, you can use context.Background() to create an empty top-level context. This context is globally independent and does not contain any values.

ctx := context.Background()
Copy after login

You can also use the WithValue() function to create a context with a value. For example, you can create an HTTP request handling context with request data and timeout limits.

ctx := context.WithValue(
        context.Background(),
        "requestId",
        uuid.New())
Copy after login

ctx.Value() function uses this context to get the value of context. In the example below, we can get Context information by requesting a unique identifier.

requestId, ok := ctx.Value("requestId").(value string)
Copy after login
  1. Timeout signal

Using the context.WithDeadline or context.WithTimeout function, you can also apply a timeout signal to the context to avoid long-running processes.

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Copy after login

The cancel() function is used to mark the cancellation status of the Context. The Context will be automatically canceled when a timeout event occurs.

select {
case <-timeOutCtx.Done():
         if err := timeOutCtx.Err(); err != nil {
                   fmt.Println("Time out due to: ", err)
         }
case <-time.After(5 * time.Second):
       fmt.Println("Hooray! Request done within 5 sec")
}
Copy after login

In this example, we create a 10-second timeout Context. The select statement waits for operations on two channels. The Done() method emits a signal when the Context is canceled or times out.

We send short messages through the timer channel and wait for 5 seconds. Since the second argument to our context.WithTimeout() function is 10 seconds, only the first route in the select statement should be executed.

  1. Context Cancellable

Context Used during long running processes, a feature of the cancellation signal can be used to avoid unexpected load on the system.

In the following code snippet, we will use context.WithCancel() to create a Context, and then use the cancel() function to mark the cancellation status of the Context. If the given goroutine completes before the Context is canceled, its completion signal is sent via the Done() method.

ctx, cancel := context.WithCancel(context.Background())

go func(ctx context.Context) {
      select {
             case <-ctx.Done():
                 fmt.Println("Exiting goroutine")
                 return
             default:
                  fmt.Println("Processing...")
       }
}(ctx)

// Exit after 5 sec
time.AfterFunc(5*time.Second, cancel)
Copy after login

Here, we use Done() and default branch in goroutine. If the Context is canceled or times out, the Done() method returns a signal and calls the cancel() function to cancel the goroutine's running.

In the main function, we use the time.AfterFunc() function to call the cancellation() function of this Context to mark the cancellation status of the Context. This will trigger the goroutine cancellation after 5 seconds.

  1. Context超时和取消

在处理请求的时间,我们通常需要确保 goroutine 不会无限期地等待,而需要在可接受的时间范围内执行操作。

在下面的代码段中,我们将使用 context.WithTimeout() 函数创建一个带有 5 秒超时限制的 Context。

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

select {
    case <-time.After(5 * time.Second):
       fmt.Println("Request completed")
    case <-ctx.Done():
         fmt.Println("Exit due to: ", ctx.Err())
}
Copy after login

我们也使用了 cancel() 函数,确保 Context 被取消时自动触发。

为了模拟一个长时间的操作,我们使用 time.After(channel)。 当 goroutine 执行时间超过 2 秒时,Context 始终会被取消。 select 语句通过检查两个 channel 的操作结果而“安全地”退出。

总结

在 Go 语言中,Context 是通用工具,用于管理请求范围的数据。它提供了一种非常强大,灵活的方法,以跨 API 边界传递请求范围的值,如取消信号、截止日期、请求 ID 等。

在本文中,我们深入探讨了 Go Context 的一些实际用例,并讨论了一些最佳实践,以优化应用程序的可维护性和性能。

随着应用程序和网络的规模增长,Context 的正确使用和管理变得非常重要。如果用得当,它可以提高应用程序的健壮性和性能,从而确保进行细粒度的请求管理。

The above is the detailed content of Learn more about the usage of Go Context. 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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

PostgreSQL monitoring method under Debian PostgreSQL monitoring method under Debian Apr 02, 2025 am 07:27 AM

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

See all articles