Skip to content

Commit

Permalink
Singleflight the identify calls
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickxb committed Dec 2, 2015
1 parent 951244e commit 0a9fd85
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 19 deletions.
54 changes: 35 additions & 19 deletions go/service/identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
package service

import (
"fmt"
"time"

"github.com/golang/groupcache/singleflight"
"golang.org/x/net/context"
"stathat.com/c/ramcache"

Expand All @@ -29,6 +31,7 @@ type IdentifyHandler struct {
*BaseHandler
libkb.Contextified
resultCache *ramcache.Ramcache
callGroup singleflight.Group
}

func NewIdentifyHandler(xp rpc.Transporter, g *libkb.GlobalContext) *IdentifyHandler {
Expand All @@ -44,34 +47,47 @@ func NewIdentifyHandler(xp rpc.Transporter, g *libkb.GlobalContext) *IdentifyHan
}

func (h *IdentifyHandler) Identify(_ context.Context, arg keybase1.IdentifyArg) (keybase1.IdentifyRes, error) {
if arg.Source == keybase1.IdentifySource_KBFS {
h.G().Log.Debug("KBFS Identify: checking result cache for %q", arg.UserAssertion)
x, err := h.resultCache.Get(arg.UserAssertion)
if err == nil {
exp, ok := x.(*keybase1.IdentifyRes)
if ok {
h.G().Log.Debug("KBFS Identify: found cached result for %q", arg.UserAssertion)
return *exp, nil
var do = func() (interface{}, error) {
if arg.Source == keybase1.IdentifySource_KBFS {
h.G().Log.Debug("KBFS Identify: checking result cache for %q", arg.UserAssertion)
x, err := h.resultCache.Get(arg.UserAssertion)
if err == nil {
exp, ok := x.(*keybase1.IdentifyRes)
if ok {
h.G().Log.Debug("KBFS Identify: found cached result for %q", arg.UserAssertion)
return *exp, nil
}
}
h.G().Log.Debug("KBFS Identify: no cached result for %q", arg.UserAssertion)
}
h.G().Log.Debug("KBFS Identify: no cached result for %q", arg.UserAssertion)

res, err := h.identify(arg.SessionID, arg)
if err != nil {
return keybase1.IdentifyRes{}, err
}
exp := res.Export()

if len(arg.UserAssertion) > 0 {
if err := h.resultCache.Set(arg.UserAssertion, exp); err != nil {
h.G().Log.Debug("Identify: result cache set error: %s", err)
} else {
h.G().Log.Debug("Identify: storing result for %q in result cache", arg.UserAssertion)
}
}

return *exp, nil
}

res, err := h.identify(arg.SessionID, arg)
v, err := h.callGroup.Do(arg.UserAssertion, do)
if err != nil {
return keybase1.IdentifyRes{}, err
}

exp := res.Export()
if len(arg.UserAssertion) > 0 {
if err := h.resultCache.Set(arg.UserAssertion, exp); err != nil {
h.G().Log.Debug("Identify: result cache set error: %s", err)
} else {
h.G().Log.Debug("Identify: storing result for %q in result cache", arg.UserAssertion)
}
res, ok := v.(keybase1.IdentifyRes)
if !ok {
return keybase1.IdentifyRes{}, fmt.Errorf("invalid type returned by do: %T", v)
}

return *exp, nil
return res, nil
}

func (h *IdentifyHandler) makeContext(sessionID int, arg keybase1.IdentifyArg) (ret *engine.Context, err error) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions go/vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
"revision": "c20a8bde38c8f5ba06f6600edf473705c96829d1",
"revisionTime": "2015-08-24T01:38:10-07:00"
},
{
"path": "github.com/golang/groupcache/singleflight",
"revision": "604ed5785183e59ae2789449d89e73f3a2a77987",
"revisionTime": "2015-01-25T10:08:32-08:00"
},
{
"path": "github.com/google/go-snappy/snappy",
"revision": "eaa750b9bf4dcb7cb20454be850613b66cda3273",
Expand Down

0 comments on commit 0a9fd85

Please sign in to comment.