git » chasquid » commit a5ea6c9

aliases: Add files unconditionally, to allow for reloads

author Alberto Bertogli
2016-09-25 19:33:54 UTC
committer Alberto Bertogli
2016-10-09 23:51:04 UTC
parent 469cbd5d761561522d4ddb4fc333a2f4cd9f17d8

aliases: Add files unconditionally, to allow for reloads

Today, if the aliases file does not exist when chasquid starts up, the entire
domain will be skipped from aliases resolution.

That's a bug, as it means we don't perform character and suffix replacements
for known domains, and is also an inconvenience as it forces us to reload the
daemon when adding a file for a known domain.

This patch fixes this by adding them unconditionally, even if the file does
not exist.

chasquid.go +5 -7
internal/aliases/aliases.go +17 -4

diff --git a/chasquid.go b/chasquid.go
index adbfae6..ea3c18a 100644
--- a/chasquid.go
+++ b/chasquid.go
@@ -159,12 +159,10 @@ func loadDomain(name, dir string, s *Server, aliasesR *aliases.Resolver) {
 		}
 	}
 
-	if _, err := os.Stat(dir + "/aliases"); err == nil {
-		glog.Infof("    adding aliases")
-		err := aliasesR.AddAliasesFile(name, dir+"/aliases")
-		if err != nil {
-			glog.Errorf("      error: %v", err)
-		}
+	glog.Infof("    adding aliases")
+	err := aliasesR.AddAliasesFile(name, dir+"/aliases")
+	if err != nil {
+		glog.Errorf("      error: %v", err)
 	}
 }
 
@@ -276,7 +274,7 @@ func (s *Server) InitQueue(path string, aliasesR *aliases.Resolver,
 	// Launch the periodic reload of aliases, now that the queue may care
 	// about them.
 	go func() {
-		for range time.Tick(1 * time.Minute) {
+		for range time.Tick(30 * time.Second) {
 			err := aliasesR.Reload()
 			if err != nil {
 				glog.Errorf("Error reloading aliases: %v")
diff --git a/internal/aliases/aliases.go b/internal/aliases/aliases.go
index d251f48..e633b21 100644
--- a/internal/aliases/aliases.go
+++ b/internal/aliases/aliases.go
@@ -173,16 +173,26 @@ func (v *Resolver) AddDomain(domain string) {
 }
 
 func (v *Resolver) AddAliasesFile(domain, path string) error {
+	// We inconditionally add the domain and file on our list.
+	// Even if the file does not exist now, it may later. This makes it be
+	// consider when doing Reload.
+	// Adding it to the domains mean that we will do drop character and suffix
+	// manipulation even if there are no aliases for it.
+	v.mu.Lock()
+	v.files[domain] = append(v.files[domain], path)
+	v.domains[domain] = true
+	v.mu.Unlock()
+
 	aliases, err := parseFile(domain, path)
+	if os.IsNotExist(err) {
+		return nil
+	}
 	if err != nil {
 		return err
 	}
 
-	v.mu.Lock()
-	v.files[domain] = append(v.files[domain], path)
-	v.domains[domain] = true
-
 	// Add the aliases to the resolver, overriding any previous values.
+	v.mu.Lock()
 	for addr, rs := range aliases {
 		v.aliases[addr] = rs
 	}
@@ -201,6 +211,9 @@ func (v *Resolver) Reload() error {
 	for domain, paths := range v.files {
 		for _, path := range paths {
 			aliases, err := parseFile(domain, path)
+			if os.IsNotExist(err) {
+				continue
+			}
 			if err != nil {
 				return fmt.Errorf("Error parsing %q: %v", path, err)
 			}