1
package mox
2
3
import (
4
"context"
5
"sync/atomic"
6
"time"
7
8
"github.com/mjl-/mox/mlog"
9
)
10
11
var cid atomic.Int64
12
13
func init() {
14
cid.Store(time.Now().UnixMilli())
15
}
16
17
// Cid returns a new unique id to be used for connections/sessions/requests.
18
func Cid() int64 {
19
return cid.Add(1)
20
}
21
22
// CidFromCtx returns the cid in the context, or 0.
23
func 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