1package mox
2
3import (
4 "context"
5 "sync/atomic"
6 "time"
7
8 "github.com/mjl-/mox/mlog"
9)
10
11var cid atomic.Int64
12
13func init() {
14 cid.Store(time.Now().UnixMilli())
15}
16
17// Cid returns a new unique id to be used for connections/sessions/requests.
18func Cid() int64 {
19 return cid.Add(1)
20}
21
22// CidFromCtx returns the cid in the context, or 0.
23func CidFromCtx(ctx context.Context) int64 {
24 v := ctx.Value(mlog.CidKey)
25 if v == nil {
26 return 0
27 }
28 return v.(int64)
29}
30