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: b79d222de9b3030c663f5d5ecfc308646bcbdf09 (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
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 if it exists
func (d *Disk) GetDomain(host string) (*domain.Domain, error) {
	host = strings.ToLower(host)

	d.lock.RLock()
	defer d.lock.RUnlock()

	domain, _ := d.dm[host]

	return domain, nil
}

// 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
}