2018-10-29 01:49:45 +03:00
|
|
|
package server
|
2016-12-06 20:30:48 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"os"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func bsonID() string {
|
|
|
|
b := make([]byte, 12)
|
|
|
|
binary.BigEndian.PutUint32(b, uint32(time.Now().Unix()))
|
|
|
|
copy(b[4:], bsonMachine)
|
|
|
|
binary.BigEndian.PutUint32(b[8:], atomic.AddUint32(&bsonCounter, 1))
|
|
|
|
binary.BigEndian.PutUint16(b[7:], bsonProcess)
|
|
|
|
return hex.EncodeToString(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
bsonProcess = uint16(os.Getpid())
|
|
|
|
bsonMachine = func() []byte {
|
2022-09-24 23:57:03 +03:00
|
|
|
host, _ := os.Hostname()
|
|
|
|
b := make([]byte, 3)
|
|
|
|
Must(rand.Read(b))
|
|
|
|
host = Default(host, string(b))
|
2016-12-06 20:30:48 +03:00
|
|
|
hw := md5.New()
|
|
|
|
hw.Write([]byte(host))
|
|
|
|
return hw.Sum(nil)[:3]
|
|
|
|
}()
|
|
|
|
bsonCounter = func() uint32 {
|
|
|
|
b := make([]byte, 4)
|
2022-09-24 23:57:03 +03:00
|
|
|
Must(rand.Read(b))
|
2016-12-06 20:30:48 +03:00
|
|
|
return binary.BigEndian.Uint32(b)
|
|
|
|
}()
|
|
|
|
)
|