我正在实现一个 TCP 连接池以记录到流利的位,这是代码
import (
"fmt"
"log"
"net"
"sync"
)
type FluentConnectionPool struct {
sync.Mutex
pool *sync.Pool
}
func (fl *FluentConnectionPool) Log(message string) {
fl.Lock()
defer fl.Unlock()
conn := fl.pool.Get().(*net.TCPConn)
defer fl.pool.Put(conn)
fmt.Printf("using: %v\n", conn.LocalAddr())
if _, err := conn.Write([]byte(message)) ; err != nil {
log.Fatal(err)
}
}
func (fl *FluentConnectionPool) Close() {
conn := fl.pool.Get().(*net.TCPConn)
defer conn.Close()
fmt.Printf("Closing: %v\n", conn.LocalAddr())
}
func New(address string) (*FluentConnectionPool, error) {
fluentAddress, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, err
}
pool := &sync.Pool{New: func() interface{} {
connection, _ := net.DialTCP("tcp", nil, fluentAddress)
return connection
}}
return &FluentConnectionPool{
pool: pool,
}, nil
}
当我像这样测试代码时
import "time"
func main() {
pool, _ := New("localhost:5170")
defer pool.Close()
for i := 0 ; i < 10 ; i++ {
go func() {
pool.Log(`{"data": {"name": "name here"}}`)
}()
}
time.Sleep(1 * time.Second)
}
输出是这样的
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43990
using: 127.0.0.1:43994
Closing: 127.0.0.1:43994
我不明白为什么连接创建两次(43990 和 43994)即使我已经锁定了功能,所以 43990 上的连接仍然打开,你能解释一下为什么会这样吗?
隔江千里
相关分类