https://github.com/liou2021/tcp-pool
https://github.com/liou2021/tcp-pool
golang pool ring-buffer tcp-pool
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/liou2021/tcp-pool
- Owner: LIOU2021
- License: mit
- Created: 2023-04-16T12:27:40.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-09-19T03:21:50.000Z (over 2 years ago)
- Last Synced: 2025-01-28T04:29:14.140Z (over 1 year ago)
- Topics: golang, pool, ring-buffer, tcp-pool
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# introduction
- 底层采用带有头尾指标的ring buffer循环利用记忆体
# ref
- [golang 环形队列](https://juejin.cn/s/golang%20%E7%8E%AF%E5%BD%A2%E9%98%9F%E5%88%97)
- [go语言数据结构 环形队列](https://cloud.tencent.com/developer/article/1165585)
# tcp connect pool
# example
```go
package main
import (
"fmt"
"log"
"net"
"time"
"github.com/LIOU2021/tcp-pool/pool"
)
func main() {
address := "127.0.0.1:8000"
pool := &pool.ConnPool{
Dial: func() (net.Conn, error) {
return net.Dial("tcp", address)
},
MaxIdle: 10,
MinIdle: 2,
IdleTime: 10 * time.Second,
}
pool.CreatePool()
conn, err := pool.Get()
if err != nil {
log.Fatal(err)
}
conn.Write([]byte(fmt.Sprintf("echo %s", "hello world")))
buf := make([]byte, 4096)
cnt, err := conn.Read(buf)
if err != nil {
fmt.Printf("Fail to read from server: %s\n", err)
}
fmt.Println(string(buf[0:cnt]))
pool.Put(conn)
pool.Release()
}
```