* 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
185 lines
4.3 KiB
Go
185 lines
4.3 KiB
Go
// mgo - MongoDB driver for Go
|
|
//
|
|
// Copyright (c) 2010-2012 - Gustavo Niemeyer <gustavo@niemeyer.net>
|
|
//
|
|
// All rights reserved.
|
|
//
|
|
// Redistribution and use in source and binary forms, with or without
|
|
// modification, are permitted provided that the following conditions are met:
|
|
//
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
|
// list of conditions and the following disclaimer.
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
// and/or other materials provided with the distribution.
|
|
//
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
package mgo
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var stats *Stats
|
|
var statsMutex sync.Mutex
|
|
|
|
// SetStats enable database state monitoring
|
|
func SetStats(enabled bool) {
|
|
statsMutex.Lock()
|
|
if enabled {
|
|
if stats == nil {
|
|
stats = &Stats{}
|
|
}
|
|
} else {
|
|
stats = nil
|
|
}
|
|
statsMutex.Unlock()
|
|
}
|
|
|
|
// GetStats return the current database state
|
|
func GetStats() (snapshot Stats) {
|
|
statsMutex.Lock()
|
|
snapshot = *stats
|
|
statsMutex.Unlock()
|
|
return
|
|
}
|
|
|
|
// ResetStats reset Stats to the previous database state
|
|
func ResetStats() {
|
|
statsMutex.Lock()
|
|
debug("Resetting stats")
|
|
old := stats
|
|
stats = &Stats{}
|
|
// These are absolute values:
|
|
stats.Clusters = old.Clusters
|
|
stats.SocketsInUse = old.SocketsInUse
|
|
stats.SocketsAlive = old.SocketsAlive
|
|
stats.SocketRefs = old.SocketRefs
|
|
statsMutex.Unlock()
|
|
return
|
|
}
|
|
|
|
// Stats holds info on the database state
|
|
//
|
|
// Relevant documentation:
|
|
//
|
|
// https://docs.mongodb.com/manual/reference/command/serverStatus/
|
|
//
|
|
// TODO outdated fields ?
|
|
type Stats struct {
|
|
Clusters int
|
|
MasterConns int
|
|
SlaveConns int
|
|
SentOps int
|
|
ReceivedOps int
|
|
ReceivedDocs int
|
|
SocketsAlive int
|
|
SocketsInUse int
|
|
SocketRefs int
|
|
TimesSocketAcquired int
|
|
TimesWaitedForPool int
|
|
TotalPoolWaitTime time.Duration
|
|
PoolTimeouts int
|
|
}
|
|
|
|
func (stats *Stats) cluster(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.Clusters += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) conn(delta int, master bool) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
if master {
|
|
stats.MasterConns += delta
|
|
} else {
|
|
stats.SlaveConns += delta
|
|
}
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) sentOps(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.SentOps += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) receivedOps(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.ReceivedOps += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) receivedDocs(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.ReceivedDocs += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) socketsInUse(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.SocketsInUse += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) socketsAlive(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.SocketsAlive += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) socketRefs(delta int) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.SocketRefs += delta
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) noticeSocketAcquisition(waitTime time.Duration) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.TimesSocketAcquired++
|
|
stats.TotalPoolWaitTime += waitTime
|
|
if waitTime > 0 {
|
|
stats.TimesWaitedForPool++
|
|
}
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|
|
|
|
func (stats *Stats) noticePoolTimeout(waitTime time.Duration) {
|
|
if stats != nil {
|
|
statsMutex.Lock()
|
|
stats.TimesWaitedForPool++
|
|
stats.PoolTimeouts++
|
|
stats.TotalPoolWaitTime += waitTime
|
|
statsMutex.Unlock()
|
|
}
|
|
}
|