No Alibaba Cloud SDK V1.0 for Go, é possível configurar períodos de timeout para solicitações de conexão e leitura em diferentes níveis, desde requisições individuais até padrões globais.
Métodos
Prioridade do timeout de leitura (da maior para a menor): objeto de requisição > cliente do SDK > objeto Config > padrão da biblioteca principal > padrão global.
Prioridade do timeout de conexão (da maior para a menor): objeto de requisição > cliente do SDK > padrão global.
-
Utilize as configurações padrão. O timeout de conexão padrão é de 5.000 milissegundos e o timeout de leitura padrão é de 10.000 milissegundos.
ImportanteA biblioteca principal do Alibaba Cloud SDK for Go inclui valores predefinidos de timeout de leitura para algumas operações de API. Quando existe um valor predefinido, ele tem precedência sobre o padrão global. Para mais informações, consulte api_timeout.go.
-
Use um objeto de requisição. Esta configuração se aplica apenas à requisição atual.
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) } -
Inicialize um cliente do SDK. Essa definição vale para todas as requisições enviadas por meio desse cliente.
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) } -
Especifique um objeto Config ao inicializar o cliente do SDK. Os parâmetros definidos afetam todos os clientes inicializados com esse objeto.
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) }