From 9b5007975572375f0659b759618fe34bdb513282 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Thu, 23 Mar 2017 13:58:15 -0600 Subject: [PATCH] spike a quick cache without expiration --- lfsapi/lfsapi.go | 2 +- lfsapi/ssh.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lfsapi/lfsapi.go b/lfsapi/lfsapi.go index bf33f858..d225d9aa 100644 --- a/lfsapi/lfsapi.go +++ b/lfsapi/lfsapi.go @@ -74,7 +74,7 @@ func NewClient(osEnv Env, gitEnv Env) (*Client, error) { creds := &commandCredentialHelper{ SkipPrompt: !osEnv.Bool("GIT_TERMINAL_PROMPT", true), } - sshResolver := &sshAuthClient{os: osEnv} + sshResolver := withSSHCache(&sshAuthClient{os: osEnv}) c := &Client{ Endpoints: NewEndpointFinder(gitEnv), diff --git a/lfsapi/ssh.go b/lfsapi/ssh.go index c055cd47..37fb98a3 100644 --- a/lfsapi/ssh.go +++ b/lfsapi/ssh.go @@ -16,6 +16,31 @@ type SSHResolver interface { Resolve(Endpoint, string) (sshAuthResponse, error) } +func withSSHCache(ssh SSHResolver) SSHResolver { + return &sshCache{ + endpoints: make(map[string]*sshAuthResponse), + ssh: ssh, + } +} + +type sshCache struct { + endpoints map[string]*sshAuthResponse + ssh SSHResolver +} + +func (c *sshCache) Resolve(e Endpoint, method string) (sshAuthResponse, error) { + key := strings.Join([]string{e.SshUserAndHost, e.SshPort, e.SshPath, method}, "//") + if res, ok := c.endpoints[key]; ok { + return *res, nil + } + + res, err := c.ssh.Resolve(e, method) + if err == nil { + c.endpoints[key] = &res + } + return res, err +} + type sshAuthResponse struct { Message string `json:"-"` Href string `json:"href"`