This topic describes how to enable the concurrency feature of an SDK client for Alibaba Cloud Classic SDK for Go.
Usage notes:
Concurrency is a built-in feature of Go. We recommend that you control the concurrent requests of the Classic SDK for Go at the application layer.
The Classic SDK for Go provides a method for you to directly send concurrent requests. You can control these concurrent requests in the Classic SDK for Go.
package main
import (
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"os"
)
func main() {
// Optional. The maximum number of concurrent requests. Default value: 5.
poolSize := 2
// Optional. The maximum number of requests that can be cached. Default value: 1000.
maxTaskQueueSize := 5
// Enable the asynchronous call feature when you create an SDK client.
config := sdk.NewConfig().WithEnableAsync(true).WithGoRoutinePoolSize(poolSize).WithMaxTaskQueueSize(maxTaskQueueSize)
// Initialize the credential.
credential := &credentials.AccessKeyCredential{AccessKeyId: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), AccessKeySecret: os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")}
ecsClient, err := ecs.NewClientWithOptions("cn-beijing", config, credential)
if err != nil {
fmt.Printf("Error : %v\n", err)
return
}
// You can also enable the asynchronous call feature after you initialize the SDK client.
ecsClient.EnableAsync(poolSize, maxTaskQueueSize)
}