All Products
Search
Document Center

Alibaba Cloud SDK:Configure a timeout period

Last Updated:Jul 23, 2024

This topic describes how to configure a timeout period in Alibaba Cloud SDK V1.0 for Go.

Methods

Note

The priority levels of methods that are used to configure a timeout period for read requests are listed in descending order: use a request object, initialize an SDK client, use a Config object when you initialize the SDK client, use the default configuration in the core library, and use the default settings.

The priority levels of methods that are used to configure a timeout period for connection requests are listed in descending order: use a request object, initialize an SDK client, and use the default settings.

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

    Important

    In the core library of Alibaba Cloud SDK for Go, a specific configuration is used to configure a timeout period for read requests of some API operations. If the specific configuration exists, the specific configuration is used as the default configuration. For more information, see api_timeout.go.

  • Use a request object. This method takes effect only for 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 method takes effect for all requests that are sent by using 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 method takes effect for all SDK clients that are initialized by using 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)
    }