* WIP: start mongo engine * WIP: mongo accessor and admin * integrate mongo store to main * disable mongo tests by default, only in CI * connection with constructor * add mongo buffered writer * buffered mongo writer * fix nil responses on an empty list from mongo * missing mongo index for scores * cancelable store * add gridfs implementation of avatar store * fix race on mongo session copy * gridfs avatars without tmp files * move avatar store * minor comments and refactoring for avatar store * merged from current master * simplify gridfs reader * lint: fix minor warns * test mongo against env defined url * pass MONGO_REMARK_TEST to docker and travis * set dockerfile env for mongo test url * increase connect timeout in mongo tests * pass MONGO_REMARK_TEST to drone build * add MONGO_REMARK_TEST to branch stage of drone * mass mongo test url via build_args_from_env * populate mongo IP to docker build hosts * test env * pass mongo ip via .mongo * remove .mongo temp from git * add .mongo -> env to linter step * allow more time to autoflush writer test * default mongo tests to "mongo" if not in env * merge fresh master into * add test for mongo cleanup * msg for a failed test * lazy fix for failed test * add an ability to skip all mongo tests * add backend dev instructions * remove unused code from mongo server * move mongo testing to connection_test * restore testing.go * lint: minor warns for testing code
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package mgo
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// coarseTimeProvider provides a periodically updated (approximate) time value to
|
|
// amortise the cost of frequent calls to time.Now.
|
|
//
|
|
// A read throughput increase of ~6% was measured when using coarseTimeProvider with the
|
|
// high-precision event timer (HPET) on FreeBSD 11.1 and Go 1.10.1 after merging
|
|
// #116.
|
|
//
|
|
// Calling Now returns a time.Time that is updated at the configured interval,
|
|
// however due to scheduling the value may be marginally older than expected.
|
|
//
|
|
// coarseTimeProvider is safe for concurrent use.
|
|
type coarseTimeProvider struct {
|
|
once sync.Once
|
|
stop chan struct{}
|
|
last atomic.Value
|
|
}
|
|
|
|
// Now returns the most recently acquired time.Time value.
|
|
func (t *coarseTimeProvider) Now() time.Time {
|
|
return t.last.Load().(time.Time)
|
|
}
|
|
|
|
// Close stops the periodic update of t.
|
|
//
|
|
// Any subsequent calls to Now will return the same value forever.
|
|
func (t *coarseTimeProvider) Close() {
|
|
t.once.Do(func() {
|
|
close(t.stop)
|
|
})
|
|
}
|
|
|
|
// newcoarseTimeProvider returns a coarseTimeProvider configured to update at granularity.
|
|
func newcoarseTimeProvider(granularity time.Duration) *coarseTimeProvider {
|
|
t := &coarseTimeProvider{
|
|
stop: make(chan struct{}),
|
|
}
|
|
|
|
t.last.Store(time.Now())
|
|
|
|
go func() {
|
|
ticker := time.NewTicker(granularity)
|
|
for {
|
|
select {
|
|
case <-t.stop:
|
|
ticker.Stop()
|
|
return
|
|
case <-ticker.C:
|
|
t.last.Store(time.Now())
|
|
}
|
|
}
|
|
}()
|
|
|
|
return t
|
|
}
|