github.com/gogf/gf/v2@v2.7.4/container/gpool/gpool_z_example_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gpool_test 8 9 import ( 10 "database/sql" 11 "fmt" 12 "time" 13 14 "github.com/gogf/gf/v2/container/gpool" 15 ) 16 17 func ExampleNew() { 18 type DBConn struct { 19 Conn *sql.Conn 20 } 21 22 dbConnPool := gpool.New(time.Hour, 23 func() (interface{}, error) { 24 dbConn := new(DBConn) 25 return dbConn, nil 26 }, 27 func(i interface{}) { 28 // sample : close db conn 29 // i.(DBConn).Conn.Close() 30 }) 31 32 fmt.Println(dbConnPool.TTL) 33 34 // Output: 35 // 1h0m0s 36 } 37 38 func ExamplePool_Put() { 39 type DBConn struct { 40 Conn *sql.Conn 41 Limit int 42 } 43 44 dbConnPool := gpool.New(time.Hour, 45 func() (interface{}, error) { 46 dbConn := new(DBConn) 47 dbConn.Limit = 10 48 return dbConn, nil 49 }, 50 func(i interface{}) { 51 // sample : close db conn 52 // i.(DBConn).Conn.Close() 53 }) 54 55 // get db conn 56 conn, _ := dbConnPool.Get() 57 // modify this conn's limit 58 conn.(*DBConn).Limit = 20 59 60 // example : do same db operation 61 // conn.(*DBConn).Conn.QueryContext(context.Background(), "select * from user") 62 63 // put back conn 64 dbConnPool.MustPut(conn) 65 66 fmt.Println(conn.(*DBConn).Limit) 67 68 // Output: 69 // 20 70 } 71 72 func ExamplePool_Clear() { 73 type DBConn struct { 74 Conn *sql.Conn 75 Limit int 76 } 77 78 dbConnPool := gpool.New(time.Hour, 79 func() (interface{}, error) { 80 dbConn := new(DBConn) 81 dbConn.Limit = 10 82 return dbConn, nil 83 }, 84 func(i interface{}) { 85 i.(*DBConn).Limit = 0 86 // sample : close db conn 87 // i.(DBConn).Conn.Close() 88 }) 89 90 conn, _ := dbConnPool.Get() 91 dbConnPool.MustPut(conn) 92 dbConnPool.MustPut(conn) 93 fmt.Println(dbConnPool.Size()) 94 dbConnPool.Clear() 95 fmt.Println(dbConnPool.Size()) 96 97 // Output: 98 // 2 99 // 0 100 } 101 102 func ExamplePool_Get() { 103 type DBConn struct { 104 Conn *sql.Conn 105 Limit int 106 } 107 108 dbConnPool := gpool.New(time.Hour, 109 func() (interface{}, error) { 110 dbConn := new(DBConn) 111 dbConn.Limit = 10 112 return dbConn, nil 113 }, 114 func(i interface{}) { 115 // sample : close db conn 116 // i.(DBConn).Conn.Close() 117 }) 118 119 conn, err := dbConnPool.Get() 120 if err == nil { 121 fmt.Println(conn.(*DBConn).Limit) 122 } 123 124 // Output: 125 // 10 126 } 127 128 func ExamplePool_Size() { 129 type DBConn struct { 130 Conn *sql.Conn 131 Limit int 132 } 133 134 dbConnPool := gpool.New(time.Hour, 135 func() (interface{}, error) { 136 dbConn := new(DBConn) 137 dbConn.Limit = 10 138 return dbConn, nil 139 }, 140 func(i interface{}) { 141 // sample : close db conn 142 // i.(DBConn).Conn.Close() 143 }) 144 145 conn, _ := dbConnPool.Get() 146 fmt.Println(dbConnPool.Size()) 147 dbConnPool.MustPut(conn) 148 dbConnPool.MustPut(conn) 149 fmt.Println(dbConnPool.Size()) 150 151 // Output: 152 // 0 153 // 2 154 } 155 156 func ExamplePool_Close() { 157 type DBConn struct { 158 Conn *sql.Conn 159 Limit int 160 } 161 var ( 162 newFunc = func() (interface{}, error) { 163 dbConn := new(DBConn) 164 dbConn.Limit = 10 165 return dbConn, nil 166 } 167 closeFunc = func(i interface{}) { 168 fmt.Println("Close The Pool") 169 // sample : close db conn 170 // i.(DBConn).Conn.Close() 171 } 172 ) 173 dbConnPool := gpool.New(time.Hour, newFunc, closeFunc) 174 175 conn, _ := dbConnPool.Get() 176 dbConnPool.MustPut(conn) 177 178 dbConnPool.Close() 179 180 // wait for pool close 181 time.Sleep(time.Second * 1) 182 183 // May Output: 184 // Close The Pool 185 }