Merge branch 'master' into 4717-read-only-keep-services-flag
[arvados.git] / services / keepproxy / keepproxy.go
index 7ba24809e066a62412996fe2fd2929e81e245b9a..d0af4a58ea5e7746d8243fb6272820e8c4801307 100644 (file)
@@ -14,6 +14,8 @@ import (
        "net/http"
        "os"
        "os/signal"
+       "reflect"
+       "regexp"
        "sync"
        "syscall"
        "time"
@@ -135,24 +137,25 @@ type ApiTokenCache struct {
 
 // Refresh the keep service list every five minutes.
 func RefreshServicesList(kc *keepclient.KeepClient) {
-       previousRoots := ""
+       var previousRoots = []map[string]string{}
+       var delay time.Duration = 0
        for {
+               time.Sleep(delay * time.Second)
+               delay = 300
                if err := kc.DiscoverKeepServers(); err != nil {
                        log.Println("Error retrieving services list:", err)
-                       time.Sleep(3*time.Second)
-                       previousRoots = ""
-               } else if len(kc.LocalRoots()) == 0 {
-                       log.Println("Received empty services list")
-                       time.Sleep(3*time.Second)
-                       previousRoots = ""
-               } else {
-                       newRoots := fmt.Sprint("Locals ", kc.LocalRoots(), ", gateways ", kc.GatewayRoots())
-                       if newRoots != previousRoots {
-                               log.Println("Updated services list:", newRoots)
-                               previousRoots = newRoots
-                       }
-                       time.Sleep(300*time.Second)
+                       delay = 3
+                       continue
+               }
+               newRoots := []map[string]string{kc.LocalRoots(), kc.GatewayRoots()}
+               if !reflect.DeepEqual(previousRoots, newRoots) {
+                       log.Printf("Updated services list: locals %v gateways %v", newRoots[0], newRoots[1])
                }
+               if len(newRoots[0]) == 0 {
+                       log.Print("WARNING: No local services. Retrying in 3 seconds.")
+                       delay = 3
+               }
+               previousRoots = newRoots
        }
 }
 
@@ -295,6 +298,8 @@ var BadAuthorizationHeader = errors.New("Missing or invalid Authorization header
 var ContentLengthMismatch = errors.New("Actual length != expected content length")
 var MethodNotSupported = errors.New("Method not supported")
 
+var removeHint, _ = regexp.Compile("\\+K@[a-z0-9]{5}(\\+|$)")
+
 func (this GetBlockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
        SetCorsHeaders(resp)
 
@@ -327,6 +332,8 @@ func (this GetBlockHandler) ServeHTTP(resp http.ResponseWriter, req *http.Reques
 
        var reader io.ReadCloser
 
+       locator = removeHint.ReplaceAllString(locator, "$1")
+
        switch req.Method {
        case "HEAD":
                expectLength, proxiedURI, err = kc.Ask(locator)