Skip to content

Instantly share code, notes, and snippets.

@fabiante
Created April 9, 2023 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiante/b87e2fcf4107f46a8e7c61bf5078fe40 to your computer and use it in GitHub Desktop.
Save fabiante/b87e2fcf4107f46a8e7c61bf5078fe40 to your computer and use it in GitHub Desktop.
MS Graph SDK Go Client Lock
package ms
import (
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
msgraphsdkgo "github.com/microsoftgraph/msgraph-sdk-go"
"sync"
)
// graphServiceClientFactoryLock is used to ensure that only one instance of any msgraphsdkgo.GraphServiceClient
// is created at a time. This is necessary because the standard constructor functions do not seem to be thread safe.
var graphServiceClientFactoryLock sync.Mutex
// NewGraphServiceClientWithCredentials
//
// See graphServiceClientFactoryLock on why this function exists.
func NewGraphServiceClientWithCredentials(credential azcore.TokenCredential, scopes []string) (*msgraphsdkgo.GraphServiceClient, error) {
graphServiceClientFactoryLock.Lock()
defer graphServiceClientFactoryLock.Unlock()
return msgraphsdkgo.NewGraphServiceClientWithCredentials(credential, scopes)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment