Welcome to mirror list, hosted at ThFree Co, Russian Federation.

disk.go « disk « source « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a3ae8901d643281140e33f42331d52f0b4c2a241 (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
66
package disk

import (
	"strings"
	"sync"
	"time"

	"gitlab.com/gitlab-org/gitlab-pages/internal/domain"
)

// Disk struct represents a map of all domains supported by pages that are
// stored on a disk with corresponding `config.json`.
type Disk struct {
	dm   Map
	lock *sync.RWMutex
}

// New is a factory method for the Disk source. It is initializing a mutex. It
// should not initialize `dm` as we later check the readiness by comparing it
// with a nil value.
func New() *Disk {
	return &Disk{
		lock: &sync.RWMutex{},
	}
}

// GetDomain returns a domain from the domains map
func (d *Disk) 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 *Disk) HasDomain(host string) bool {
	d.lock.RLock()
	defer d.lock.RUnlock()

	host = strings.ToLower(host)
	_, isPresent := d.dm[host]

	return isPresent
}

// IsReady checks if the domains source is ready for work. The disk source is
// ready after traversing entire filesystem and reading all domains'
// configuration files.
func (d *Disk) IsReady() bool {
	return d.dm != nil
}

// Read starts the domain source, in this case it is reading domains from
// groups on disk concurrently.
func (d *Disk) Read(rootDomain string) {
	go Watch(rootDomain, d.updateDomains, time.Second)
}

func (d *Disk) updateDomains(dm Map) {
	d.lock.Lock()
	defer d.lock.Unlock()

	d.dm = dm
}