Skip to content

Commit

Permalink
Doing synchronous cache filling for the .info endpoint
Browse files Browse the repository at this point in the history
This patch shows how we could synchronously fill in storage when the proxy or registry receive a request to '.../version.info' and the package isn't in storage.

Requires gomods#291

Part of gomods#290
  • Loading branch information
arschles committed Jul 20, 2018
1 parent 93069bd commit f8cffc6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
35 changes: 29 additions & 6 deletions pkg/download/version_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/bketelsen/buffet"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
"github.com/gomods/athens/pkg/module"
"github.com/gomods/athens/pkg/paths"
"github.com/gomods/athens/pkg/storage"
)
Expand All @@ -16,26 +17,48 @@ import (
const PathVersionInfo = "/{module:.+}/@v/{version}.info"

// VersionInfoHandler implements GET baseURL/module/@v/version.info
func VersionInfoHandler(getter storage.Getter, eng *render.Engine) func(c buffalo.Context) error {
func VersionInfoHandler(
getterSaver storage.GetterSaver,
fetcher module.Fetcher,
eng *render.Engine,
) func(c buffalo.Context) error {
return func(c buffalo.Context) error {
sp := buffet.SpanFromContext(c)
sp.SetOperationName("versionInfoHandler")
params, err := paths.GetAllParams(c)
if err != nil {
return err
}
version, err := getter.Get(params.Module, params.Version)
version, err := getterSaver.Get(params.Module, params.Version)
if storage.IsNotFoundError(err) {
msg := fmt.Sprintf("%s@%s not found", params.Module, params.Version)
return c.Render(http.StatusNotFound, eng.JSON(msg))
// TODO: serialize cache fills (https://github.com/gomods/athens/issues/308)
ref, err := fetcher.Fetch(params.Module, params.Version)
if err != nil {
// TODO: some way to figure out whether the package actually doesn't exist
return err
}
defer ref.Clear()
ver, err := ref.Read()
if err != nil {
return err
}
getterSaver.Save(c, params.Module, params.Version, ver.Mod, ver.Zip, ver.Info)
version = ver
} else if err != nil {
return err
}
var revInfo storage.RevInfo
err = json.Unmarshal(version.Info, &revInfo)
revInfo, err := parseRevInfo(version.Info)
if err != nil {
return err
}
return c.Render(http.StatusOK, eng.JSON(revInfo))
}
}

func parseRevInfo(b []byte) (*storage.RevInfo, error) {
revInfo := &storage.RevInfo{}
if err := json.Unmarshal(b, revInfo); err != nil {
return nil, err
}
return revInfo, nil
}
7 changes: 7 additions & 0 deletions pkg/storage/getter_saver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package storage

// GetterSaver is a Getter and a Saver in one
type GetterSaver interface {
Getter
Saver
}

0 comments on commit f8cffc6

Please sign in to comment.