Skip to content

Commit

Permalink
allow basic logging for http Get err with RegisterErrLogFn
Browse files Browse the repository at this point in the history
rename to follow hook function name convention, log more ctx info
  • Loading branch information
Qiao Liang committed Apr 12, 2017
1 parent 72d04f9 commit eaf5270
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions groupcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package groupcache

import (
"errors"
"fmt"
"math/rand"
"strconv"
"sync"
Expand Down Expand Up @@ -60,6 +61,7 @@ var (

initPeerServerOnce sync.Once
initPeerServer func()
errLogHook func(error)
)

// GetGroup returns the named group previously created with NewGroup, or
Expand Down Expand Up @@ -121,6 +123,20 @@ func RegisterNewGroupHook(fn func(*Group)) {
newGroupHook = fn
}

// RegisterErrLogHook registers a log func that is used for HTTP error logging.
func RegisterErrLogHook(fn func(error)) {
if errLogHook != nil {
panic("RegisterErrLogHook called more than once")
}
errLogHook = fn
}

func logErr(err error) {
if errLogHook != nil {
errLogHook(err)
}
}

// RegisterServerStart registers a hook that is run when the first
// group is created.
func RegisterServerStart(fn func()) {
Expand Down Expand Up @@ -271,10 +287,7 @@ func (g *Group) load(ctx Context, key string, dest Sink) (value ByteView, destPo
return value, nil
}
g.Stats.PeerErrors.Add(1)
// TODO(bradfitz): log the peer's error? keep
// log of the past few for /groupcachez? It's
// probably boring (normal task movement), so not
// worth logging I imagine.
logErr(fmt.Errorf("%+v, %+v, %+v, %+v", ctx, peer, key, err))
}
value, err = g.getLocally(ctx, key, dest)
if err != nil {
Expand Down

0 comments on commit eaf5270

Please sign in to comment.