Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanvc committed Jan 10, 2024
1 parent 3dd282e commit 91bcff4
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions examples/metricapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.uber.org/fx"
"google.golang.org/grpc/codes"
"log/slog"
"time"
)

func main() {
Expand All @@ -30,21 +31,24 @@ type userController struct {
}

func (controller *userController) QueryUser(c context.Context, req *QueryUserReq) (resp *UserDto, err error) {
// query redis cache, case can downgrade
resp, err = controller.queryUserFromCache(c, req)
switch base.Code(err) {
case codes.OK:
plog.ReportEvent(c, "UserFoundInCache")
return
case codes.NotFound:
plog.ReportEvent(c, "UserNotFoundInCache")
default:
plog.ReportErrEvent(c, "UserCacheUnknownErr")
slog.ErrorContext(c, "UserCacheUnknownErr")
slog.ErrorContext(c, "UserCacheUnknownErr", plog.Error(err))
}
return
}

func (controller *userController) queryUserFromCache(c context.Context, req *QueryUserReq) (resp *UserDto, err error) {
c = plog.WithLogContext(c, nil)
c, cancel := context.WithTimeoutCause(c, time.Millisecond*100,
base.New(codes.DeadlineExceeded, "GetFromRedisTimeout").Err())
defer cancel()
cmd := controller.redisCli.Get(c, fmt.Sprintf("a_%d", req.Uid))
if cmd.Err() != nil {
return nil, cmd.Err()
Expand Down

0 comments on commit 91bcff4

Please sign in to comment.