Skip to content
Snippets Groups Projects
Commit 9a0d435d authored by Z.J. van de Weg's avatar Z.J. van de Weg
Browse files

Block serving requests until first full update

Given that this might lead to 404 responses eventhough the request is
valid, we should wait until the first full update cycle is done.
parent 9851a84c
No related branches found
No related tags found
1 merge request!16Remove sleep when starting tests
Pipeline #
Loading
Loading
@@ -94,6 +94,9 @@ func (a *theApp) UpdateDomains(domains domains) {
func (a *theApp) Run() {
var wg sync.WaitGroup
 
lastUpdate := []byte("first-update")
updateDomains(a.Domain, a.UpdateDomains, &lastUpdate)
// Listen for HTTP
for _, fd := range a.ListenHTTP {
wg.Add(1)
Loading
Loading
@@ -143,8 +146,7 @@ func (a *theApp) Run() {
}
}(a.ListenMetrics)
}
go watchDomains(a.Domain, a.UpdateDomains, time.Second)
go watchDomains(a.Domain, a.UpdateDomains, time.Second, &lastUpdate)
 
wg.Wait()
}
Loading
Loading
Loading
Loading
@@ -124,38 +124,39 @@ func (d domains) ReadGroups(rootDomain string) error {
return nil
}
 
func watchDomains(rootDomain string, updater domainsUpdater, interval time.Duration) {
lastUpdate := []byte("no-update")
// We should block other goroutines until the first update has been completed
func watchDomains(rootDomain string, updater domainsUpdater, interval time.Duration, lastUpdate *[]byte) {
for {
// Read the update file
update, err := ioutil.ReadFile(".update")
if err != nil && !os.IsNotExist(err) {
log.Println("Failed to read update timestamp:", err)
}
time.Sleep(interval)
updateDomains(rootDomain, updater, lastUpdate)
}
}
 
// If it's the same ignore
if bytes.Equal(lastUpdate, update) {
time.Sleep(interval)
continue
}
lastUpdate = update
func updateDomains(rootDomain string, updater domainsUpdater, lastUpdate *[]byte) {
// Read the update file
update, err := ioutil.ReadFile(".update")
if err != nil && !os.IsNotExist(err) {
log.Println("Failed to read update timestamp:", err)
}
 
started := time.Now()
domains := make(domains)
domains.ReadGroups(rootDomain)
duration := time.Since(started)
log.Println("Updated", len(domains), "domains in", duration, "Hash:", update)
if bytes.Equal(*lastUpdate, update) {
return
}
 
if updater != nil {
updater(domains)
}
*lastUpdate = update
 
// Update prometheus metrics
metrics.DomainLastUpdateTime.Set(float64(time.Now().UTC().Unix()))
metrics.DomainsServed.Set(float64(len(domains)))
metrics.DomainUpdates.Inc()
started := time.Now()
domains := make(domains)
domains.ReadGroups(rootDomain)
duration := time.Since(started)
log.Println("Updated", len(domains), "domains in", duration, "Hash:", update)
 
time.Sleep(interval)
if updater != nil {
updater(domains)
}
// Update prometheus metrics
metrics.DomainLastUpdateTime.Set(float64(time.Now().UTC().Unix()))
metrics.DomainsServed.Set(float64(len(domains)))
metrics.DomainUpdates.Inc()
}
Loading
Loading
@@ -52,9 +52,10 @@ func TestWatchDomains(t *testing.T) {
setUpTests()
 
update := make(chan domains)
lastUpdate := []byte("no-update")
go watchDomains("gitlab.io", func(domains domains) {
update <- domains
}, time.Microsecond*50)
}, time.Microsecond*50, &lastUpdate)
 
defer os.Remove(updateFile)
 
Loading
Loading
Loading
Loading
@@ -47,6 +47,7 @@ var InsecureHTTPSClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 10 * time.Second,
}
 
var CertificateFixture = `-----BEGIN CERTIFICATE-----
Loading
Loading
@@ -153,11 +154,9 @@ func RunPagesProcess(t *testing.T, pagesPath string, listeners []ListenSpec, pro
// for now. Without it, intermittent failures occur.
//
// TODO: replace this with explicit status from the pages binary
// TODO: fix the first-request race
for _, spec := range listeners {
spec.WaitUntilListening()
}
time.Sleep(500 * time.Millisecond)
 
return func() {
cmd.Process.Signal(os.Interrupt)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment