-
Notifications
You must be signed in to change notification settings - Fork 10
/
ua_parser.go
69 lines (60 loc) · 1.04 KB
/
ua_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package statsig
import (
"sync"
"github.com/ua-parser/uap-go/uaparser"
)
type uaParser struct {
parser *uaparser.Parser
wg sync.WaitGroup
options UAParserOptions
mu sync.RWMutex
}
func newUAParser(options UAParserOptions) *uaParser {
uaParser := &uaParser{
parser: nil,
wg: sync.WaitGroup{},
options: options,
}
uaParser.delayedSetup()
return uaParser
}
func (u *uaParser) isReady() bool {
u.mu.RLock()
defer u.mu.RUnlock()
return u.parser != nil
}
func (u *uaParser) delayedSetup() {
if u.options.Disabled {
return
}
u.wg.Add(1)
go func() {
defer u.wg.Done()
u.mu.Lock()
u.parser = uaparser.NewFromSaved()
u.mu.Unlock()
}()
}
func (u *uaParser) init() {
if !u.options.LazyLoad {
u.ensureLoaded()
}
}
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.ensureLoaded()
}
if u.isReady() {
return u.parser.Parse(ua)
}
return nil
}