Skip to content

Commit

Permalink
improve asset service logging (#2290)
Browse files Browse the repository at this point in the history
  • Loading branch information
srliao authored Dec 5, 2024
1 parent 82415ef commit 5e91432
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions internal/services/assets/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ func (s *Server) handleGetData(t AssetType) http.HandlerFunc {
return
}

// check if it's a recognized item; if not then we can skip the rest
assetName, ok := s.assetNameMapping[t][key]
if !ok {
s.logger.Info("unrecognized key, serving default", "t", t.String(), "key", key)
s.handleNotFound(w)
return
}

// try cache first
data, err := s.loadFromCache(t, key)
switch {
Expand All @@ -47,25 +55,23 @@ func (s *Server) handleGetData(t AssetType) http.HandlerFunc {
s.logger.Info("cache data not found", "t", t.String(), "key", key)
}

if assetName, ok := s.assetNameMapping[t][key]; ok {
hosts := s.hosts[t]
// else try external 1 at a time
for i, v := range hosts {
joinedURL := v.JoinPath(fmt.Sprintf("/%v.png", assetName))
s.logger.Info("trying external image source", "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String())
data, err := s.proxyImageRequest(joinedURL)
if err != nil {
s.logger.Info("error getting", "err", err, "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String())
continue
}
s.logger.Info("received image from source ok", "host", v.String())
s.saveToCache(t, key, data)
// found ok, save to cache and end request
w.WriteHeader(http.StatusOK)
w.Write(data)
return
// else try external 1 at a time
for i, v := range s.hosts[t] {
joinedURL := v.JoinPath(fmt.Sprintf("/%v.png", assetName))
s.logger.Info("trying external image source", "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String())
data, err := s.proxyImageRequest(joinedURL)
if err != nil {
s.logger.Info("error getting", "err", err, "host", v.String(), "try", i, "key", key, "full_path", joinedURL.String())
continue
}
s.logger.Info("received image from source ok", "host", v.String())
s.saveToCache(t, key, data)
// found ok, save to cache and end request
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
s.logger.Info("no external source found, serving default", "t", t.String(), "key", key)
// if we reached here then all external failed so we should serve default question mark image
s.handleNotFound(w)
}
Expand Down

0 comments on commit 5e91432

Please sign in to comment.