Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection always create new connection TIME_WAIT #6

Open
gananggww opened this issue Sep 25, 2023 · 2 comments
Open

connection always create new connection TIME_WAIT #6

gananggww opened this issue Sep 25, 2023 · 2 comments

Comments

@gananggww
Copy link

gananggww commented Sep 25, 2023

usage :

main.go

func init() {
	var err error

	initCap := os.Getenv("INIT_ESTABLISH_SOCKET")
	maxCap := os.Getenv("MAX_ESTABLISH_SOCKET")

	_initCap, _ := strconv.Atoi(initCap)
	_maxCap, _ := strconv.Atoi(maxCap)

	pl, err = pool.New(_initCap, _maxCap, func() interface{} {
		addr, _ := net.ResolveTCPAddr("tcp4", os.Getenv("HSM_HOST"))
		cli, err := net.DialTCP("tcp4", nil, addr)
		if err != nil {
			log.Printf("create client connection error: %v\n", err)
		}

		return cli
	})

	if err != nil {
		log.Printf("create pool error: %v\n", err)
	}

	pl.Ping = func(conn interface{}) bool {
		return true
	}

	pl.Close = func(conn interface{}) {
		conn.(*net.TCPConn).Close()
	}

}

and this in my service called by controller

func hsmSend(pl *pool.Pool, payload string) (result []byte, err error) {
	c, err := pl.Get()
	if err != nil {
		err = fmt.Errorf("using pool error: %v", err)
		return
	}

	conn := c.(*net.TCPConn)

	dataLen := len(payload)
	x := rune(dataLen >> 8)
	b := rune(dataLen & 255)
	payloadParsed := fmt.Sprintf("%c%c%s", x, b, payload)
	conn.Write([]byte(payloadParsed))
	result = make([]byte, globalhelpers.HsmLen(payload[4:6]).(int))

	n, err := conn.Read(result)
	if err != nil || n < 4 {
		err = fmt.Errorf("read data error: %v, size: %d", err, n)
		return
	}

	pl.Put(conn)
	result = result[:n]

	return
}

connection always create new connection grater than max capacity pool

monitoring my connection
netstat -tupn | grep 1500

in my terminal

tcp        0      0 10.233.114.12:55708     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:39894     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54604     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54652     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:56400     10.35.65.144:1500       ESTABLISHED 52043/main          
tcp        0      0 10.233.114.12:39890     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55484     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54928     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:56156     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54810     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55014     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55468     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:39832     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54968     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55536     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55238     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55494     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54970     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55582     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54978     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:42690     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54736     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:54840     10.35.65.144:1500       TIME_WAIT   -                   
tcp        0      0 10.233.114.12:55096     10.35.65.144:1500       TIME_WAIT   -             
tcp        0      0 10.233.114.12:56400     10.35.65.144:1500       ESTABLISHED 52043/main      

how to set up connection just maximum established ?

@donchev7
Copy link

donchev7 commented Feb 5, 2024

The pool's Get function creates new resources when the pool is empty, which might not be desirable if you want to limit the total number of connections.
https://github.com/go-baa/pool/blob/main/pool.go#L66

If you want to limit the new connection (block until available) you will need to use change the code.

@hengfeiyang
Copy link
Member

You can see this line https://github.com/go-baa/pool/blob/main/pool.go#L79

It will create a new connection, when the connection exceeds the maximum limit, it will close it when put it back to the pool. We need to close every connection that exceeds the maximum limit, and close will cause this problem. TIME_WAIT is a common TCP issue.

Perhaps you can increase the maximum limit or change the code as @donchev7 suggested.

Or if you want to limit concurrent connections, you can use a channel to restrict concurrency.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants