Configure a timeout period
You can configure timeout periods for connection and read requests in Alibaba Cloud SDK V1.0 for Go at different levels, from individual requests to global defaults.
Methods
Read timeout priority (highest to lowest): request object > SDK client > Config object > core library default > global default.
Connection timeout priority (highest to lowest): request object > SDK client > global default.
-
Use the default settings. The default connection timeout is 5,000 milliseconds, and the default read timeout is 10,000 milliseconds.
ImportantThe core library of Alibaba Cloud SDK for Go includes predefined read timeout values for some API operations. When a predefined value exists, it takes precedence over the global default. For more information, see api_timeout.go.
-
Use a request object. This setting applies only to the current request.
import ( "fmt" "os" "time" "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" ) func main() { config := sdk.NewConfig() // Use the AccessKey ID and AccessKey secret of the Resource Access Management (RAM) user. credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) client, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential) if err != nil { panic(err) } // Create a request. request := ecs.CreateDescribeRegionsRequest() // Configure a timeout period for the request. This setting takes effect only for the current request. request.SetConnectTimeout(10 * time.Second) request.SetReadTimeout(5 * time.Second) // Specify HTTPS as the protocol. request.Scheme = "https" // The request parameters. request.InstanceChargeType = "PrePaid" // The billing method of the instance. request.ResourceType = "instance" // The resource type. // Send the request and obtain a response. response, err := client.DescribeRegions(request) if err != nil { fmt.Print(err.Error()) } fmt.Printf("response is %#v\n", response) } -
Initialize an SDK client. This setting applies to all requests sent through the SDK client.
import ( "fmt" "os" "time" "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" ) func main() { config := sdk.NewConfig() // Use the AccessKey ID and AccessKey secret of the RAM user. credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) client, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential) if err != nil { panic(err) } // Configure a timeout period on the SDK client. This setting takes effect on all requests that are sent by using the SDK client. client.SetConnectTimeout(10 * time.Second) client.SetReadTimeout(5 * time.Second) // Create a request. request := ecs.CreateDescribeRegionsRequest() // Specify HTTPS as the protocol. request.Scheme = "https" // The request parameters. request.InstanceChargeType = "PrePaid" // The billing method of the instance. request.ResourceType = "instance" // The resource type. // Send the request and obtain a response. response, err := client.DescribeRegions(request) if err != nil { fmt.Print(err.Error()) } fmt.Printf("response is %#v\n", response) } -
Use a Config object when you initialize the SDK client. This setting applies to all SDK clients initialized with the Config object.
import ( "fmt" "os" "time" "github.com/aliyun/alibaba-cloud-sdk-go/sdk" "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" ) func main() { config := sdk.NewConfig() // Use a Config object to configure a timeout period. config.Timeout = 5 * time.Second // Use the AccessKey ID and AccessKey secret of the RAM user. credential := credentials.NewAccessKeyCredential(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")) client, err := ecs.NewClientWithOptions("cn-hangzhou", config, credential) if err != nil { panic(err) } // Create a request. request := ecs.CreateDescribeRegionsRequest() // Specify HTTPS as the protocol. request.Scheme = "https" // The request parameters. request.InstanceChargeType = "PrePaid" // The billing method of the instance. request.ResourceType = "instance" // The resource type. // Send the request and obtain a response. response, err := client.DescribeRegions(request) if err != nil { fmt.Print(err.Error()) } fmt.Printf("response is %#v\n", response) }