Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetEndpointMap 在并发下 出现data race #523

Closed
Lvzhenqian opened this issue Aug 31, 2021 · 1 comment
Closed

GetEndpointMap 在并发下 出现data race #523

Lvzhenqian opened this issue Aug 31, 2021 · 1 comment

Comments

@Lvzhenqian
Copy link

  • 产品和接口: golang sdk
  • 平台: centos 7
  • 最小代码:
==================
WARNING: DATA RACE
Read at 0x000004cbf9d0 by goroutine 36:
  github.com/aliyun/alibaba-cloud-sdk-go/services/rds.GetEndpointMap()
      /home/charles/codes/golang/yw-assist/vendor/github.com/aliyun/alibaba-cloud-sdk-go/services/rds/endpoint.go:11 +0x3e
  github.com/aliyun/alibaba-cloud-sdk-go/services/rds.SetEndpointDataToClient()
      /home/charles/codes/golang/yw-assist/vendor/github.com/aliyun/alibaba-cloud-sdk-go/services/rds/client.go:41 +0x33
  github.com/aliyun/alibaba-cloud-sdk-go/services/rds.NewClientWithAccessKey()
      /home/charles/codes/golang/yw-assist/vendor/github.com/aliyun/alibaba-cloud-sdk-go/services/rds/client.go:82 +0x10a
  yw-assist/AliSDK.NewDefaultRdsClient()
      /home/charles/codes/golang/yw-assist/AliSDK/Rds.go:29 +0xc6
  yw-assist/AliSDK.getRdsInfo()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:28 +0x64
  yw-assist/AliSDK.getRegionInfo.func2()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:185 +0xbb

Previous write at 0x000004cbf9d0 by goroutine 37:
  github.com/aliyun/alibaba-cloud-sdk-go/services/rds.GetEndpointMap()
      /home/charles/codes/golang/yw-assist/vendor/github.com/aliyun/alibaba-cloud-sdk-go/services/rds/endpoint.go:12 +0x148
  github.com/aliyun/alibaba-cloud-sdk-go/services/rds.SetEndpointDataToClient()
      /home/charles/codes/golang/yw-assist/vendor/github.com/aliyun/alibaba-cloud-sdk-go/services/rds/client.go:41 +0x33
  github.com/aliyun/alibaba-cloud-sdk-go/services/rds.NewClientWithAccessKey()
      /home/charles/codes/golang/yw-assist/vendor/github.com/aliyun/alibaba-cloud-sdk-go/services/rds/client.go:82 +0x10a
  yw-assist/AliSDK.NewDefaultRdsClient()
      /home/charles/codes/golang/yw-assist/AliSDK/Rds.go:29 +0xc6
  yw-assist/AliSDK.getRdsInfo()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:28 +0x64
  yw-assist/AliSDK.getRegionInfo.func2()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:185 +0xbb

Goroutine 36 (running) created at:
  yw-assist/AliSDK.getRegionInfo()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:183 +0x211
  yw-assist/AliSDK.getAllRegionInfo()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:216 +0x22b
  yw-assist/AliSDK.cacheAllRegionInfoIntoKvDB()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:304 +0x91
  yw-assist/AliSDK.CacheDBInstanceInfo.func1()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:326 +0x4c

Goroutine 37 (running) created at:
  yw-assist/AliSDK.getRegionInfo()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:183 +0x211
  yw-assist/AliSDK.getAllRegionInfo()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:216 +0x22b
  yw-assist/AliSDK.cacheAllRegionInfoIntoKvDB()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:304 +0x91
  yw-assist/AliSDK.CacheDBInstanceInfo.func1()
      /home/charles/codes/golang/yw-assist/AliSDK/cache.go:326 +0x4c
==================
@ljluestc
Copy link

package AliSDK

import (
	"sync"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/rds"
)

type RdsClient struct {
	client *rds.Client
	mu     sync.Mutex // Mutex to protect the shared resource
}

func NewDefaultRdsClient() (*RdsClient, error) {
	client, err := rds.NewClientWithAccessKey("cn-hangzhou", "<YourAccessKey>", "<YourSecretKey>")
	if err != nil {
		return nil, err
	}
	return &RdsClient{client: client}, nil
}

func (r *RdsClient) GetRegionInfo() {
	r.mu.Lock()         // Locking the mutex to ensure exclusive access to the resource
	defer r.mu.Unlock() // Unlock the mutex once the function execution is done

	// Now you can safely access and modify shared data without a data race
	resp, err := r.client.DescribeRegions()
	if err != nil {
		// Handle error
	}
	// Process the response...
	_ = resp
}

func (r *RdsClient) GetEndpointMap() {
	r.mu.Lock()         // Locking the mutex to ensure exclusive access to the resource
	defer r.mu.Unlock() // Unlock the mutex once the function execution is done

	// Fetch and handle the endpoint map safely
	endpointMap, err := r.client.GetEndpointMap()
	if err != nil {
		// Handle error
	}
	// Process the endpointMap...
	_ = endpointMap
}

func main() {
	client, err := NewDefaultRdsClient()
	if err != nil {
		// Handle error
	}
	// Run concurrent goroutines, all access to GetEndpointMap will be thread-safe now
	go client.GetRegionInfo()
	go client.GetEndpointMap()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants