2023-01-30 16:27:06 +03:00
|
|
|
package mox
|
|
|
|
|
|
|
|
import (
|
2023-03-01 00:12:27 +03:00
|
|
|
"context"
|
2023-01-30 16:27:06 +03:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
2023-03-01 00:12:27 +03:00
|
|
|
|
|
|
|
"github.com/mjl-/mox/mlog"
|
2023-01-30 16:27:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var cid atomic.Int64
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cid.Store(time.Now().UnixMilli())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cid returns a new unique id to be used for connections/sessions/requests.
|
|
|
|
func Cid() int64 {
|
|
|
|
return cid.Add(1)
|
|
|
|
}
|
2023-03-01 00:12:27 +03:00
|
|
|
|
|
|
|
// CidFromCtx returns the cid in the context, or 0.
|
|
|
|
func CidFromCtx(ctx context.Context) int64 {
|
|
|
|
v := ctx.Value(mlog.CidKey)
|
|
|
|
if v == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return v.(int64)
|
|
|
|
}
|