blob: cd2d89c55f16c07029e1c2e262fb5e34d762dace (
plain)
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
|
package source
import (
"strings"
"sync"
"time"
"gitlab.com/gitlab-org/gitlab-pages/internal/domain"
"gitlab.com/gitlab-org/gitlab-pages/internal/source/disk"
)
// Domains struct represents a map of all domains supported by pages. It is
// currently reading them from disk.
type Domains struct {
dm disk.Map
lock *sync.RWMutex
}
// NewDomains is a factory method for domains initializing a mutex. It should
// not initialize `dm` as we later check the readiness by comparing it with a
// nil value.
func NewDomains() *Domains {
return &Domains{
lock: &sync.RWMutex{},
}
}
// GetDomain returns a domain from the domains map
func (d *Domains) GetDomain(host string) *domain.Domain {
host = strings.ToLower(host)
d.lock.RLock()
defer d.lock.RUnlock()
domain, _ := d.dm[host]
return domain
}
// HasDomain checks for presence of a domain in the domains map
func (d *Domains) HasDomain(host string) bool {
d.lock.RLock()
defer d.lock.RUnlock()
host = strings.ToLower(host)
_, isPresent := d.dm[host]
return isPresent
}
// Ready checks if the domains source is ready for work
func (d *Domains) Ready() bool {
return d.dm != nil
}
// Watch starts the domain source, in this case it is reading domains from
// groups on disk concurrently
func (d *Domains) Watch(rootDomain string) {
go disk.Watch(rootDomain, d.updateDomains, time.Second)
}
func (d *Domains) updateDomains(dm disk.Map) {
d.lock.Lock()
defer d.lock.Unlock()
d.dm = dm
}
|