-
-
Save mrb/5630865 to your computer and use it in GitHub Desktop.
Channels for Pool! Cool Idea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type InitFunction func() (interface{}, error) | |
| type ConnectionPoolWrapper struct { | |
| size int | |
| conn chan interface{} | |
| } | |
| func (p *ConnectionPoolWrapper) InitPool(size int, initfn InitFunction) error { | |
| p.conn = make(chan interface{}, size) | |
| for x := 0; x < size; x++ { | |
| fmt.Println("Calling init function ", x) | |
| conn, err := initfn() | |
| if err != nil { | |
| return err | |
| } | |
| p.conn <- conn | |
| } | |
| p.size = size | |
| return nil | |
| } | |
| func (p *ConnectionPoolWrapper) GetConnection() interface{} { | |
| return <-p.conn | |
| } | |
| func (p *ConnectionPoolWrapper) ReleaseConnection(conn interface{}) { | |
| p.conn <- conn | |
| } | |
| var dbPool = &ConnectionPoolWrapper{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment