├── .travis.yml ├── LICENSE ├── README.md ├── capped.go ├── capped_test.go ├── example_test.go ├── interface.go ├── interface_test.go ├── leaky.go ├── leaky_test.go └── script └── test /.travis.yml: -------------------------------------------------------------------------------- 1 | language: go 2 | go: 1.6 3 | 4 | services: 5 | - mongodb 6 | 7 | branches: 8 | only: 9 | - master 10 | 11 | env: 12 | - MONGO_HOST=127.0.0.1 13 | 14 | script: script/test 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Visual Supply, Co. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## ***** DEPRECATION NOTICE ****** 2 | The mongo library that this project depends on has been deprecated ([mgo](https://github.com/globalsign/mgo)), in favor of the 3 | [official driver released by mongodb](https://github.com/mongodb/mongo-go-driver). The official driver includes an internal 4 | mongo connection pool by default, which obviates this library. 5 | 6 | 7 | # mgopool
[![GoDoc](https://godoc.org/github.com/vsco/mgopool?status.svg)](https://godoc.org/github.com/vsco/mgopool) [![Build Status](https://travis-ci.org/vsco/mgopool.svg?branch=master)](https://travis-ci.org/vsco/mgopool) 8 | 9 | Package mgopool provides buffer implementations around [mgo.Session (v2)][mgo]. 10 | 11 | ``` 12 | go get github.com/globalsign/mgo 13 | go get github.com/vsco/mgopool 14 | ``` 15 | 16 | With mgo, an initial session is created with the server, and then either a [Copy][Session.Copy] or [Clone][Session.Clone] 17 | of that initial session is used for each unit of work (HTTP request handler, worker loop, etc.), which is 18 | [closed][Session.Close] upon completion. This allows mgo to efficiently manage a pool of sockets between those sessions; 19 | however, it also results in the underlying sockets to constantly log in and out of the Mongo cluster. 20 | 21 | mgopool avoids this issue by creating a Pool of copies from the initial session, storing them in a free list. Work units 22 | request an existing copy (or one is created if it doesn't exist) and then return it to the pool on completion. Both a 23 | leaky and capped version of the Pool is available, depending on need. 24 | 25 | ## Example Usage 26 | 27 | ```go 28 | func Example() { 29 | // create the initial mgo.Session, defer closing it 30 | initial, _ := mgo.Dial("127.0.0.1") 31 | defer initial.Close() 32 | 33 | // create a new leaky pool with size of 3 34 | pool := mgopool.NewLeaky(initial, 3) 35 | defer pool.Close() 36 | 37 | wg := sync.WaitGroup{} 38 | 39 | // create a few workers to utilize the sessions in the pool 40 | for i := 0; i < 3; i++ { 41 | wg.Add(1) 42 | 43 | go func() { 44 | for j := 0; j < 2; j++ { 45 | // get a session from the pool 46 | session := pool.Get() 47 | 48 | // do something with that session, refresh it if there is an error 49 | if err := doSomething(session); err != nil { 50 | session.Refresh() 51 | } 52 | 53 | // return the session to the pool 54 | pool.Put(session) 55 | } 56 | wg.Done() 57 | }() 58 | } 59 | 60 | // wait for the workers to complete 61 | wg.Wait() 62 | 63 | // Output: 64 | // foobar 65 | // foobar 66 | // foobar 67 | // foobar 68 | // foobar 69 | // foobar 70 | } 71 | 72 | func doSomething(s *mgo.Session) error { 73 | fmt.Println("foobar") 74 | return s.Ping() 75 | } 76 | ``` 77 | 78 | [mgo]: https://godoc.org/github.com/globalsign/mgo 79 | [Session.Copy]: https://godoc.org/github.com/globalsign/mgo#Session.Copy 80 | [Session.Clone]: https://godoc.org/github.com/globalsign/mgo#Session.Clone 81 | [Session.Close]: https://godoc.org/github.com/globalsign/mgo#Session.Close 82 | 83 | ## Testing 84 | 85 | Testing mgopool requires a running Mongo cluster. The default Mongo host is used (`127.0.0.1:27017`) and 86 | can be overridden by setting the `MONGO_HOST` env variable. Execute the tests with the following command from the project root: 87 | 88 | ```sh 89 | script/test 90 | ``` 91 | 92 | ## License 93 | 94 | The MIT License (MIT) 95 | 96 | Copyright (c) 2016 Visual Supply, Co. 97 | 98 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 99 | 100 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 101 | 102 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 103 | -------------------------------------------------------------------------------- /capped.go: -------------------------------------------------------------------------------- 1 | package mgopool 2 | 3 | import mgo "github.com/globalsign/mgo" 4 | 5 | type capped struct { 6 | pool Pool 7 | leases chan struct{} 8 | } 9 | 10 | // NewCapped creates a capped Pool of sessions copied from initial. Get on a capped Pool will block after size sessions 11 | // have been retrieved until one is Put back in. 12 | func NewCapped(initial *mgo.Session, size int) Pool { 13 | return &capped{ 14 | pool: NewLeaky(initial, size), 15 | leases: make(chan struct{}, size), 16 | } 17 | } 18 | 19 | func (p *capped) Get() *mgo.Session { 20 | p.leases <- struct{}{} 21 | return p.pool.Get() 22 | } 23 | 24 | func (p *capped) Put(s *mgo.Session) { 25 | select { 26 | case <-p.leases: 27 | default: 28 | //if leases is empty, continue anyways 29 | } 30 | 31 | p.pool.Put(s) 32 | } 33 | 34 | func (p *capped) Close() { 35 | close(p.leases) 36 | p.pool.Close() 37 | } 38 | 39 | func (p *capped) Used() int { 40 | return p.pool.Used() 41 | } 42 | 43 | var _ Pool = &capped{} 44 | -------------------------------------------------------------------------------- /capped_test.go: -------------------------------------------------------------------------------- 1 | package mgopool 2 | 3 | import ( 4 | "testing" 5 | 6 | "time" 7 | 8 | mgo "github.com/globalsign/mgo" 9 | "github.com/stretchr/testify/assert" 10 | ) 11 | 12 | func TestCapped_Get(t *testing.T) { 13 | orig := session(t) 14 | defer orig.Close() 15 | p := NewCapped(orig, 1) 16 | defer p.Close() 17 | 18 | expected := p.Get() 19 | var actual *mgo.Session 20 | 21 | done := make(chan struct{}) 22 | go func() { 23 | actual = p.Get() 24 | close(done) 25 | }() 26 | 27 | p.Put(expected) 28 | 29 | select { 30 | case <-done: 31 | case <-time.NewTimer(time.Millisecond * 10).C: 32 | t.Fatal("capped bucket never unblocked") 33 | } 34 | 35 | assert.Equal(t, expected, actual) 36 | } 37 | 38 | func TestCapped_Put(t *testing.T) { 39 | orig := session(t) 40 | defer orig.Close() 41 | p := NewCapped(orig, 1) 42 | defer p.Close() 43 | 44 | expected := p.Get() 45 | 46 | p.Put(expected) 47 | p.Put(orig.Copy()) 48 | 49 | actual := p.Get() 50 | assert.Equal(t, expected, actual) 51 | 52 | done := make(chan struct{}) 53 | go func() { 54 | actual = p.Get() 55 | close(done) 56 | }() 57 | 58 | p.Put(expected) 59 | 60 | select { 61 | case <-done: 62 | case <-time.NewTimer(time.Millisecond * 10).C: 63 | t.Fatal("capped bucket never unblocked") 64 | } 65 | 66 | assert.Equal(t, expected, actual) 67 | } 68 | 69 | func TestCapped_Close(t *testing.T) { 70 | orig := session(t) 71 | defer orig.Close() 72 | p := NewCapped(orig, 1) 73 | 74 | s := p.Get() 75 | 76 | p.Put(s) 77 | p.Close() 78 | 79 | assert.Panics(t, func() { 80 | p.Get() 81 | }) 82 | 83 | assert.Panics(t, func() { 84 | p.Put(s) 85 | }) 86 | 87 | assert.Panics(t, func() { 88 | s.Copy() 89 | }) 90 | 91 | assert.NotPanics(t, func() { 92 | orig.Copy().Close() 93 | }) 94 | } 95 | 96 | func TestCapped_Used(t *testing.T) { 97 | orig := session(t) 98 | defer orig.Close() 99 | 100 | p := NewCapped(orig, 1) 101 | defer p.Close() 102 | assert.Equal(t, 0, p.Used()) 103 | 104 | leak := p.Get() 105 | assert.Equal(t, 1, p.Used()) 106 | 107 | p.Put(leak) 108 | assert.Equal(t, 0, p.Used()) 109 | } 110 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | package mgopool 2 | 3 | import ( 4 | "os" 5 | 6 | "sync" 7 | 8 | "fmt" 9 | 10 | "github.com/globalsign/mgo" 11 | ) 12 | 13 | func Example() { 14 | host := os.Getenv("MONGO_HOST") 15 | 16 | // create the initial mgo.Session, defer closing it 17 | initial, _ := mgo.Dial(host) 18 | defer initial.Close() 19 | 20 | // create a new leaky pool with size of 3 21 | pool := NewLeaky(initial, 3) 22 | defer pool.Close() 23 | 24 | wg := sync.WaitGroup{} 25 | 26 | // create a few workers to utilize the sessions in the pool 27 | for i := 0; i < 3; i++ { 28 | wg.Add(1) 29 | 30 | go func() { 31 | for j := 0; j < 2; j++ { 32 | // get a session from the pool 33 | session := pool.Get() 34 | 35 | // do something with that session, refresh it if there is an error 36 | if err := doSomething(session); err != nil { 37 | session.Refresh() 38 | } 39 | 40 | // return the session to the pool 41 | pool.Put(session) 42 | } 43 | wg.Done() 44 | }() 45 | } 46 | 47 | // wait for the workers to complete 48 | wg.Wait() 49 | 50 | // Output: 51 | // foobar 52 | // foobar 53 | // foobar 54 | // foobar 55 | // foobar 56 | // foobar 57 | } 58 | 59 | func doSomething(s *mgo.Session) error { 60 | fmt.Println("foobar") 61 | return s.Ping() 62 | } 63 | -------------------------------------------------------------------------------- /interface.go: -------------------------------------------------------------------------------- 1 | // Package mgopool provides buffer implementations around mgo.Session (v2). 2 | // 3 | // With mgo, an initial session is created with the server, and then either a copy or clone of that initial session is 4 | // used for each unit of work (HTTP request handler, worker loop, etc.), which is closed upon completion. This allows 5 | // mgo to efficiently manage a pool of sockets between those sessions; however, this results in the underlying sockets 6 | // to constantly log in and out of the Mongo cluster. 7 | // 8 | // mgopool avoids this issue by creating a Pool of copies from the initial session, storing them in a free list. Work 9 | // units request an existing copy (or one is created if it doesn't exist) and then return it to the pool on completion. 10 | // Both a leaky and capped version of the Pool is available, depending on need. 11 | package mgopool 12 | 13 | import mgo "github.com/globalsign/mgo" 14 | 15 | // The Pool interface describes the mechanisms for retrieving and releasing mgo.Sessions. The pool should be used in 16 | // place of calling Copy/Clone and Close on mgo.Session directly. 17 | type Pool interface { 18 | // Get returns a *mgo.Session from the Pool. If there are no free sessions, a new one is copied from the initial 19 | // Session. If the Pool is capped and all sessions have been retrieved, Get will block until a session is returned to 20 | // with Put. 21 | Get() *mgo.Session 22 | 23 | // Put releases an *mgo.Session back into the Pool. Only healthy sessions (or nil) should be returned to the pool. If 24 | // a session is errors, Refresh should be called before releasing it to the Pool. 25 | Put(*mgo.Session) 26 | 27 | // Used is the total number of outstanding leases associated with this Pool. 28 | // Note, this is just a snapshot of the used leased at the time this method was invoked 29 | Used() int 30 | 31 | // Close drains the Pool, closing all held sessions. The initial session is not closed by this method. Calling Get or 32 | // Put on a closed pool will result in a panic. 33 | Close() 34 | } 35 | -------------------------------------------------------------------------------- /interface_test.go: -------------------------------------------------------------------------------- 1 | package mgopool 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | 7 | "github.com/globalsign/mgo" 8 | ) 9 | 10 | func session(t *testing.T) *mgo.Session { 11 | host := os.Getenv("MONGO_HOST") 12 | 13 | sess, err := mgo.Dial(host) 14 | if err != nil { 15 | t.Fatalf("%v", err) 16 | } 17 | 18 | return sess 19 | } 20 | -------------------------------------------------------------------------------- /leaky.go: -------------------------------------------------------------------------------- 1 | package mgopool 2 | 3 | import ( 4 | "sync/atomic" 5 | 6 | mgo "github.com/globalsign/mgo" 7 | ) 8 | 9 | type leaky struct { 10 | base *mgo.Session 11 | freeList chan *mgo.Session 12 | used int32 13 | } 14 | 15 | // NewLeaky creates a leaky Pool of sessions copied from initial. A maximum of size sessions will be held in the free 16 | // list, but the Pool will not block calls to Get. Releasing excess sessions to the pool are automatically Closed and 17 | // removed. 18 | func NewLeaky(initial *mgo.Session, size int) Pool { 19 | return &leaky{ 20 | base: initial.Clone(), 21 | freeList: make(chan *mgo.Session, size), 22 | used: 0, 23 | } 24 | 25 | } 26 | 27 | func (p *leaky) Get() *mgo.Session { 28 | atomic.AddInt32(&p.used, 1) 29 | select { 30 | case s, more := <-p.freeList: 31 | if s == nil || !more { 32 | panic("pool has been closed") 33 | } 34 | return s 35 | default: 36 | // if freeList is empty, Copy a new session 37 | return p.base.Copy() 38 | } 39 | } 40 | 41 | func (p *leaky) Put(s *mgo.Session) { 42 | atomic.AddInt32(&p.used, -1) 43 | if s == nil { 44 | return 45 | } 46 | 47 | select { 48 | case p.freeList <- s: 49 | default: 50 | // if freeList is full, close and discard the session 51 | s.Close() 52 | } 53 | } 54 | 55 | func (p *leaky) Close() { 56 | close(p.freeList) 57 | for s := range p.freeList { 58 | s.Close() 59 | } 60 | p.base.Close() 61 | } 62 | 63 | func (p *leaky) Used() int { 64 | return int(p.used) 65 | } 66 | 67 | var _ Pool = &leaky{} 68 | -------------------------------------------------------------------------------- /leaky_test.go: -------------------------------------------------------------------------------- 1 | package mgopool 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func TestLeaky_Get(t *testing.T) { 10 | orig := session(t) 11 | defer orig.Close() 12 | p := NewLeaky(orig, 1) 13 | defer p.Close() 14 | 15 | s := p.Get() 16 | assert.NotNil(t, s) 17 | 18 | p.Put(s) 19 | next := p.Get() 20 | assert.Equal(t, s, next) 21 | } 22 | 23 | func TestLeaky_Put(t *testing.T) { 24 | orig := session(t) 25 | defer orig.Close() 26 | p := NewLeaky(orig, 1) 27 | defer p.Close() 28 | 29 | leak := p.Get() 30 | 31 | p.Put(p.Get()) 32 | p.Put(leak) 33 | 34 | actual := p.Get() 35 | assert.NotEqual(t, leak, actual) 36 | 37 | p.Put(nil) 38 | actual = p.Get() 39 | 40 | assert.NotNil(t, actual) 41 | } 42 | 43 | func TestLeak_Close(t *testing.T) { 44 | orig := session(t) 45 | defer orig.Close() 46 | p := NewLeaky(orig, 1) 47 | 48 | s := p.Get() 49 | 50 | p.Put(s) 51 | p.Close() 52 | 53 | assert.Panics(t, func() { 54 | p.Get() 55 | }) 56 | 57 | assert.Panics(t, func() { 58 | p.Put(s) 59 | }) 60 | 61 | assert.Panics(t, func() { 62 | s.Copy() 63 | }) 64 | 65 | assert.NotPanics(t, func() { 66 | orig.Copy().Close() 67 | }) 68 | } 69 | 70 | func TestLeaky_Used(t *testing.T) { 71 | orig := session(t) 72 | defer orig.Close() 73 | 74 | p := NewLeaky(orig, 1) 75 | defer p.Close() 76 | assert.Equal(t, 0, p.Used()) 77 | 78 | leak := p.Get() 79 | assert.Equal(t, 1, p.Used()) 80 | 81 | p.Put(p.Get()) 82 | p.Put(leak) 83 | assert.Equal(t, 0, p.Used()) 84 | } 85 | -------------------------------------------------------------------------------- /script/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -e 3 | 4 | : ${MONGO_HOST:=127.0.0.1} 5 | 6 | test -z "$(gofmt -l -w . | tee /dev/stderr)" 7 | test -z "$(golint ./... | tee /dev/stderr)" 8 | test -z "$(go tool vet -tests . | tee /dev/stderr)" 9 | 10 | go test -v -race -cover ./... 11 | --------------------------------------------------------------------------------