Skip to content

Commit

Permalink
Allow UAParser and CountryLookup to load in case of timeout (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-statsig authored Jul 16, 2024
1 parent a17493f commit bad9d89
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func NewClientWithOptions(sdkKey string, options *Options) *Client {
}

func (c *Client) init() {
c.evaluator.initialize(c.options)
c.evaluator.initialize()
}

func (c *Client) initInBackground() {
Expand Down
21 changes: 13 additions & 8 deletions country_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func newCountryLookup(options IPCountryOptions) *countryLookup {
wg: sync.WaitGroup{},
options: options,
}
countryLookup.delayedSetup()
return countryLookup
}

Expand All @@ -28,7 +29,7 @@ func (c *countryLookup) isReady() bool {
return c.lookup != nil
}

func (c *countryLookup) init(lazyLoadOverride bool) {
func (c *countryLookup) delayedSetup() {
if c.options.Disabled {
return
}
Expand All @@ -39,15 +40,19 @@ func (c *countryLookup) init(lazyLoadOverride bool) {
c.lookup = countrylookup.New()
c.mu.Unlock()
}()
c.mu.Lock()
lazyLoad := c.options.LazyLoad
if lazyLoadOverride {
lazyLoad = lazyLoadOverride
}

func (c *countryLookup) init() {
if !c.options.LazyLoad {
c.ensureLoaded()
}
c.mu.Unlock()
if !lazyLoad {
c.wg.Wait()
}

func (c *countryLookup) ensureLoaded() {
if c.options.Disabled {
return
}
c.wg.Wait()
}

func (c *countryLookup) lookupIp(ip string) (string, bool) {
Expand Down
14 changes: 3 additions & 11 deletions evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,7 @@ func newEvaluator(
}()
persistentStorageUtils := newUserPersistentStorageUtils(options)
countryLookup := newCountryLookup(options.IPCountryOptions)
countryLookup.init(true)
uaParser := newUAParser(options.UAParserOptions)
uaParser.init(true)

return &evaluator{
store: store,
Expand All @@ -119,16 +117,10 @@ func newEvaluator(
}
}

func (e *evaluator) initialize(options *Options) {
func (e *evaluator) initialize() {
e.store.initialize()
e.initializeHeavyEvaluatorFields(options)
}

func (e *evaluator) initializeHeavyEvaluatorFields(options *Options) {
e.mu.Lock()
defer e.mu.Unlock()
e.uaParser.init(false)
e.countryLookup.init(false)
e.uaParser.init()
e.countryLookup.init()
}

func (e *evaluator) shutdown() {
Expand Down
23 changes: 14 additions & 9 deletions ua_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func newUAParser(options UAParserOptions) *uaParser {
wg: sync.WaitGroup{},
options: options,
}
uaParser.delayedSetup()
return uaParser
}

Expand All @@ -28,7 +29,7 @@ func (u *uaParser) isReady() bool {
return u.parser != nil
}

func (u *uaParser) init(lazyLoadOverride bool) {
func (u *uaParser) delayedSetup() {
if u.options.Disabled {
return
}
Expand All @@ -39,23 +40,27 @@ func (u *uaParser) init(lazyLoadOverride bool) {
u.parser = uaparser.NewFromSaved()
u.mu.Unlock()
}()
u.mu.Lock()
lazyLoad := u.options.LazyLoad
if lazyLoadOverride {
lazyLoad = lazyLoadOverride
}

func (u *uaParser) init() {
if !u.options.LazyLoad {
u.ensureLoaded()
}
u.mu.Unlock()
if !lazyLoad {
u.wg.Wait()
}

func (u *uaParser) ensureLoaded() {
if u.options.Disabled {
return
}
u.wg.Wait()
}

func (u *uaParser) parse(ua string) *uaparser.Client {
if u.options.Disabled {
return nil
}
if u.options.EnsureLoaded {
u.wg.Wait()
u.ensureLoaded()
}
if u.isReady() {
return u.parser.Parse(ua)
Expand Down

0 comments on commit bad9d89

Please sign in to comment.