Skip to content

Commit b1c5727

Browse files
make providers map concurrent safe (#521)
1 parent 0629733 commit b1c5727

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

provider.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7+
"sync"
78

89
"golang.org/x/oauth2"
910
)
@@ -23,15 +24,21 @@ type Provider interface {
2324

2425
const NoAuthUrlErrorMessage = "an AuthURL has not been set"
2526

26-
// Providers is list of known/available providers.
27+
// Providers is the list of known/available providers.
2728
type Providers map[string]Provider
2829

29-
var providers = Providers{}
30+
var (
31+
providersHat sync.RWMutex
32+
providers = Providers{}
33+
)
3034

3135
// UseProviders adds a list of available providers for use with Goth.
3236
// Can be called multiple times. If you pass the same provider more
3337
// than once, the last will be used.
3438
func UseProviders(viders ...Provider) {
39+
providersHat.Lock()
40+
defer providersHat.Unlock()
41+
3542
for _, provider := range viders {
3643
providers[provider.Name()] = provider
3744
}
@@ -45,7 +52,9 @@ func GetProviders() Providers {
4552
// GetProvider returns a previously created provider. If Goth has not
4653
// been told to use the named provider it will return an error.
4754
func GetProvider(name string) (Provider, error) {
55+
providersHat.RLock()
4856
provider := providers[name]
57+
providersHat.RUnlock()
4958
if provider == nil {
5059
return nil, fmt.Errorf("no provider for %s exists", name)
5160
}
@@ -55,6 +64,9 @@ func GetProvider(name string) (Provider, error) {
5564
// ClearProviders will remove all providers currently in use.
5665
// This is useful, mostly, for testing purposes.
5766
func ClearProviders() {
67+
providersHat.Lock()
68+
defer providersHat.Unlock()
69+
5870
providers = Providers{}
5971
}
6072

0 commit comments

Comments
 (0)