forked from mongodb/mongo-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_session.go
69 lines (55 loc) · 1.66 KB
/
server_session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package session
import (
"time"
"crypto/rand"
"go.mongodb.org/mongo-driver/x/bsonx"
"go.mongodb.org/mongo-driver/x/mongo/driver/uuid"
)
var rander = rand.Reader
// Server is an open session with the server.
type Server struct {
SessionID bsonx.Doc
TxnNumber int64
LastUsed time.Time
Dirty bool
}
// returns whether or not a session has expired given a timeout in minutes
// a session is considered expired if it has less than 1 minute left before becoming stale
func (ss *Server) expired(timeoutMinutes uint32) bool {
if timeoutMinutes <= 0 {
return true
}
timeUnused := time.Since(ss.LastUsed).Minutes()
return timeUnused > float64(timeoutMinutes-1)
}
// update the last used time for this session.
// must be called whenever this server session is used to send a command to the server.
func (ss *Server) updateUseTime() {
ss.LastUsed = time.Now()
}
func newServerSession() (*Server, error) {
id, err := uuid.New()
if err != nil {
return nil, err
}
idDoc := bsonx.Doc{{"id", bsonx.Binary(UUIDSubtype, id[:])}}
return &Server{
SessionID: idDoc,
LastUsed: time.Now(),
}, nil
}
// IncrementTxnNumber increments the transaction number.
func (ss *Server) IncrementTxnNumber() {
ss.TxnNumber++
}
// MarkDirty marks the session as dirty.
func (ss *Server) MarkDirty() {
ss.Dirty = true
}
// UUIDSubtype is the BSON binary subtype that a UUID should be encoded as
const UUIDSubtype byte = 4