Skip to content

Fix race condition in init by using sync.Once #17

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
"time"
)

Expand All @@ -25,12 +26,15 @@ func SetDefaults(variable interface{}) {
getDefaultFiller().Fill(variable)
}

var defaultFiller *Filler = nil
var (
defaultFillerOnce sync.Once
defaultFiller *Filler = nil
)

func getDefaultFiller() *Filler {
if defaultFiller == nil {
defaultFillerOnce.Do(func() {
defaultFiller = newDefaultFiller()
}
})

return defaultFiller
}
Expand Down
10 changes: 7 additions & 3 deletions factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ import (
"encoding/hex"
"math/rand"
"reflect"
"sync"
"time"
)

func Factory(variable interface{}) {
getFactoryFiller().Fill(variable)
}

var factoryFiller *Filler = nil
var (
factoryFillerOnce sync.Once
factoryFiller *Filler = nil
)

func getFactoryFiller() *Filler {
if factoryFiller == nil {
factoryFillerOnce.Do(func() {
factoryFiller = newFactoryFiller()
}
})

return factoryFiller
}
Expand Down