orlandos-nl / BSON

Native Swift library for BSON (http://bsonspec.org)
https://orlandos.nl/docs/mongokitten/articles/bson
MIT License
108 stars 36 forks source link

ObjectId.swift In linux objectid is closely link to the second of startup time only #47

Closed icyield closed 1 year ago

icyield commented 6 years ago

If a number of instance of an app are started in parallel there is a danger that if they start in the same second there can be duplicate objectIds generated.

Perhaps time in millisecs could be used.

Or is there a source of true random on linux (/dev/random)

if os(Linux)

private static var random: Int32 = {
    srand(UInt32(time(nil)))
    return rand()
}()
private static var counter: Int32 = {
    srand(UInt32(time(nil)))
    return rand()
}()
#else
Joannis commented 6 years ago

Good idea! Thanks for the input, definitely a valid oversight. For BSON 6 we could rely on the Swift 4.2 unified random integer API (in the standard library). For Linux randomness for the existing BSON (5.2) implementation we could use a PR. The second problem that comes to mind is that counter == random under the circumstances you describe.