Skip to content

Instantly share code, notes, and snippets.

@thevuuranusls
Created March 9, 2021 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thevuuranusls/d15ab59ad0fef90514b0a91a8972cd75 to your computer and use it in GitHub Desktop.
Save thevuuranusls/d15ab59ad0fef90514b0a91a8972cd75 to your computer and use it in GitHub Desktop.
func (h *serviceHandler) GetId(ctx context.Context,
req *id.Request, resp *id.Response) error {
logs.Info("GetId with request: %#v", req)
defer logs.Trace("GetId out: %#v", resp)
t := time.Now().Local()
dt := t.Format("060102") // format time yymmdd
key := fmt.Sprintf("id:%d", req.Type)
curr := h.redis.Incr(key)
val, err := curr.Val(), curr.Err()
if err != nil {
if span := opentracing.SpanFromContext(ctx); span != nil {
span.SetTag("error", req.Type)
ext.Error.Set(span, true)
}
logs.Error("Error when INCR id, error: %#v", err)
return err
}
resp.Auto = val
if req.Type != id.TYPE_NONE {
resp.Id = fmt.Sprintf("%sT%03d%08d", dt, req.Type, val)
}
if len(req.Prefix) > 0 {
//* 181030T04100003934 -> M181030-04100003934
//* 181030T04100003934 -> M181114-00000033
resp.Idx = fmt.Sprintf("%s%s-%03d%08d", req.Prefix, dt, req.Type, val)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment