Skip to content

Commit

Permalink
Merge pull request go-mgo#2 from jameinel/juju-patches
Browse files Browse the repository at this point in the history
Bring in the patches that Juju had in its patches/ directory.
  • Loading branch information
jameinel authored Nov 13, 2018
2 parents f2b6f6c + efe1c76 commit ec70493
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 101 deletions.
2 changes: 1 addition & 1 deletion bson/bson.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ var nullBytes = []byte("null")
func (id *ObjectId) UnmarshalJSON(data []byte) error {
if len(data) > 0 && (data[0] == '{' || data[0] == 'O') {
var v struct {
Id json.RawMessage `json:"$oid"`
Id json.RawMessage `json:"$oid"`
Func struct {
Id json.RawMessage
} `json:"$oidFunc"`
Expand Down
2 changes: 1 addition & 1 deletion bson/decimal_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// BSON library for Go
// BSON library for Go
//
// Copyright (c) 2010-2012 - Gustavo Niemeyer <[email protected]>
//
Expand Down
2 changes: 1 addition & 1 deletion bson/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func jencNumberLong(v interface{}) ([]byte, error) {
func jencInt(v interface{}) ([]byte, error) {
n := v.(int)
f := `{"$numberLong":"%d"}`
if n <= 1<<53 {
if int64(n) <= 1<<53 {
f = `%d`
}
return fbytes(f, n), nil
Expand Down
4 changes: 4 additions & 0 deletions cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ func (cluster *mongoCluster) AcquireSocket(mode Mode, slaveOk bool, syncTimeout
cluster.syncServers()
time.Sleep(100 * time.Millisecond)
continue
} else {
server.Lock()
server.abended = false
server.Unlock()
}
}
return s, nil
Expand Down
1 change: 0 additions & 1 deletion cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,6 @@ func (s *S) TestSecondaryModeWithMongosInsert(c *C) {
c.Assert(result.A, Equals, 1)
}


func (s *S) TestRemovalOfClusterMember(c *C) {
if *fast {
c.Skip("-fast")
Expand Down
7 changes: 6 additions & 1 deletion dbtest/dbserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,12 @@ func (dbs *DBServer) Stop() {
select {
case <-dbs.tomb.Dead():
case <-time.After(5 * time.Second):
panic("timeout waiting for mongod process to die")
dbs.server.Process.Signal(os.Kill)
select {
case <-dbs.tomb.Dead():
case <-time.After(5 * time.Second):
panic("timeout waiting for mongod process to die")
}
}
dbs.server = nil
}
Expand Down
2 changes: 1 addition & 1 deletion gridfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ func (file *GridFile) insertChunk(data []byte) {
// an error, if any.
func (file *GridFile) Seek(offset int64, whence int) (pos int64, err error) {
file.m.Lock()
debugf("GridFile %p: seeking for %s (whence=%d)", file, offset, whence)
debugf("GridFile %p: seeking for %d (whence=%d)", file, offset, whence)
defer file.m.Unlock()
switch whence {
case os.SEEK_SET:
Expand Down
6 changes: 3 additions & 3 deletions internal/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ func (d *decodeState) isNull(off int) bool {
// name consumes a const or function from d.data[d.off-1:], decoding into the value v.
// the first byte of the function name has been read already.
func (d *decodeState) name(v reflect.Value) {
if d.isNull(d.off-1) {
if d.isNull(d.off - 1) {
d.literal(v)
return
}
Expand Down Expand Up @@ -1076,9 +1076,9 @@ func (d *decodeState) storeKeyed(v reflect.Value) bool {
}

var (
trueBytes = []byte("true")
trueBytes = []byte("true")
falseBytes = []byte("false")
nullBytes = []byte("null")
nullBytes = []byte("null")
)

func (d *decodeState) storeValue(v reflect.Value, from interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion internal/json/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestDecoder(t *testing.T) {
for _, c := range nlines(streamEncoded, i) {
// That's stupid isn't it!? nulltrue!?!? :/
//if c != '\n' {
buf.WriteRune(c)
buf.WriteRune(c)
//}
}
out := make([]interface{}, i)
Expand Down
2 changes: 1 addition & 1 deletion internal/scram/scram.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (c *Client) Step(in []byte) bool {
func (c *Client) step1(in []byte) error {
if len(c.clientNonce) == 0 {
const nonceLen = 6
buf := make([]byte, nonceLen + b64.EncodedLen(nonceLen))
buf := make([]byte, nonceLen+b64.EncodedLen(nonceLen))
if _, err := rand.Read(buf[:nonceLen]); err != nil {
return fmt.Errorf("cannot read random SCRAM-SHA-1 nonce from operating system: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3268,7 +3268,7 @@ func (db *Database) run(socket *mongoSocket, cmd, result interface{}) (err error
if result != nil {
err = bson.Unmarshal(data, result)
if err != nil {
debugf("Run command unmarshaling failed: %#v", op, err)
debugf("Run command unmarshaling failed: %#v %v", op, err)
return err
}
if globalDebug && globalLogger != nil {
Expand Down
128 changes: 59 additions & 69 deletions stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,29 @@ import (
"sync"
)

var stats *Stats
var statsMutex sync.Mutex
var stats Stats

func SetStats(enabled bool) {
statsMutex.Lock()
if enabled {
if stats == nil {
stats = &Stats{}
}
} else {
stats = nil
}
statsMutex.Unlock()
stats.reset(enabled)
}

func GetStats() (snapshot Stats) {
statsMutex.Lock()
snapshot = *stats
statsMutex.Unlock()
return
func GetStats() Stats {
stats.mu.RLock()
defer stats.mu.RUnlock()
return stats
}

func ResetStats() {
statsMutex.Lock()
// If we call ResetStats we assume you want to use stats, so we enable
// them.
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.reset(true)
}

type Stats struct {
mu sync.RWMutex
enabled bool

Clusters int
MasterConns int
SlaveConns int
Expand All @@ -78,70 +64,74 @@ type Stats struct {
SocketRefs int
}

func (stats *Stats) cluster(delta int) {
if stats != nil {
statsMutex.Lock()
stats.Clusters += delta
statsMutex.Unlock()
func (stats *Stats) reset(enabled bool) {
stats.mu.Lock()
defer stats.mu.Unlock()

stats.MasterConns = 0
stats.SlaveConns = 0
stats.SentOps = 0
stats.ReceivedOps = 0
stats.ReceivedDocs = 0

if !enabled {
// These are absolute values so we don't reset them unless we are
// disabling stats altogether.
stats.Clusters = 0
stats.SocketsInUse = 0
stats.SocketsAlive = 0
stats.SocketRefs = 0
}
}

func (stats *Stats) cluster(delta int) {
stats.mu.Lock()
stats.Clusters += delta
stats.mu.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()
stats.mu.Lock()
if master {
stats.MasterConns += delta
} else {
stats.SlaveConns += delta
}
stats.mu.Unlock()
}

func (stats *Stats) sentOps(delta int) {
if stats != nil {
statsMutex.Lock()
stats.SentOps += delta
statsMutex.Unlock()
}
stats.mu.Lock()
stats.SentOps += delta
stats.mu.Unlock()
}

func (stats *Stats) receivedOps(delta int) {
if stats != nil {
statsMutex.Lock()
stats.ReceivedOps += delta
statsMutex.Unlock()
}
stats.mu.Lock()
stats.ReceivedOps += delta
stats.mu.Unlock()
}

func (stats *Stats) receivedDocs(delta int) {
if stats != nil {
statsMutex.Lock()
stats.ReceivedDocs += delta
statsMutex.Unlock()
}
stats.mu.Lock()
stats.ReceivedDocs += delta
stats.mu.Unlock()
}

func (stats *Stats) socketsInUse(delta int) {
if stats != nil {
statsMutex.Lock()
stats.SocketsInUse += delta
statsMutex.Unlock()
}
stats.mu.Lock()
stats.SocketsInUse += delta
stats.mu.Unlock()
}

func (stats *Stats) socketsAlive(delta int) {
if stats != nil {
statsMutex.Lock()
stats.SocketsAlive += delta
statsMutex.Unlock()
}
stats.mu.Lock()
stats.SocketsAlive += delta
stats.mu.Unlock()
}

func (stats *Stats) socketRefs(delta int) {
if stats != nil {
statsMutex.Lock()
stats.SocketRefs += delta
statsMutex.Unlock()
}
stats.mu.Lock()
stats.SocketRefs += delta
stats.mu.Unlock()
}
Loading

0 comments on commit ec70493

Please sign in to comment.