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

make providers map safe for concurrency #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"sync"

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

const NoAuthUrlErrorMessage = "an AuthURL has not been set"

// Providers is list of known/available providers.
// Providers is the list of known/available providers.
type Providers map[string]Provider

var providers = Providers{}
var (
providersHat sync.RWMutex
providers = Providers{}
)

// UseProviders adds a list of available providers for use with Goth.
// Can be called multiple times. If you pass the same provider more
// than once, the last will be used.
func UseProviders(viders ...Provider) {
providersHat.Lock()
defer providersHat.Unlock()

for _, provider := range viders {
providers[provider.Name()] = provider
}
Expand All @@ -45,7 +52,9 @@ func GetProviders() Providers {
// GetProvider returns a previously created provider. If Goth has not
// been told to use the named provider it will return an error.
func GetProvider(name string) (Provider, error) {
providersHat.RLock()
provider := providers[name]
providersHat.RUnlock()
if provider == nil {
return nil, fmt.Errorf("no provider for %s exists", name)
}
Expand All @@ -55,6 +64,9 @@ func GetProvider(name string) (Provider, error) {
// ClearProviders will remove all providers currently in use.
// This is useful, mostly, for testing purposes.
func ClearProviders() {
providersHat.Lock()
defer providersHat.Unlock()

providers = Providers{}
}

Expand Down
Loading