Closed yangwangxing closed 6 years ago
Hi @KinOSurvival ,
Could you provide the scenario you're facing for this to be an issue?
Thanks! Esther
expiresAtIndex := mgo.Index{ Key: []string{"expires_at"}, Unique: false, Name: "session_expires_at_index", Background: false, //mgo has a bug, ExpireAfter set as 0 will be ignored, so set it to 1 second ExpireAfter: time.Duration(1) * time.Second, }
err := collection.EnsureIndex(expiresAtIndex)
after executing this, index created, but without expireAfterSeconds settings
Hi @KinOSurvival
If the omitempty
tag is removed, all records would have a TTL of value of 0 seconds
, and they would be removed in the next TTL sweep. It's a quirk of the Go "zero value" which is a little tricky in this corner-case.
As the records are typically swept every 60 seconds within MongoDB, try using a TTL of 1 second
- it's makes little practical difference due to the periodic sweep. This works for us :)
I'm closing this as there's a reasonable solution, feel free to reply if there's a problem though!
Dom
Please add a comment to mgo.Index.ExpireAfter. It took me some time to find out the reason.
in session.go
type indexSpec struct { ... ExpireAfter int
bson:"expireAfterSeconds,omitempty"
... }
should remove omitempty in ExpireAfter's tag description, otherwise TTL index ExpireAfter will be ignored
https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time