Timeout mechanism

更新时间:
复制 MD 格式

Properly configured timeouts prevent your application from blocking indefinitely and wasting resources. The V2.0 Go SDK supports request-level and global timeout configuration.

Timeout configuration method

Note

Timeout configurations are applied in the following order of precedence, from highest to lowest: RuntimeOptions, Config, and default configurations.

  • Use the default settings. The default connection timeout is 5,000 milliseconds and the default read timeout is 10,000 milliseconds.

  • Configure the timeout for the current request using RuntimeOptions.

    import (
        util "github.com/alibabacloud-go/tea-utils/v2/service"
    )
    
    runtime := &util.RuntimeOptions{}
    // Set the timeout parameters. Unit: ms.
    runtime.ConnectTimeout = tea.Int(5000) // Set the connection timeout to 5 seconds.
    runtime.ReadTimeout = tea.Int(10000) // Set the read timeout to 10 seconds.
    
  • Configure a global timeout using Config.

    import (
        openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    )
    
    config := &openapi.Config{
        // Set the timeout parameters. Unit: ms.
        ConnectTimeout: tea.Int(5000), // Connection timeout
        ReadTimeout:    tea.Int(10000), // Read timeout
    }