Skip to content

Warmup connections #1982

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

Open
frussian opened this issue Mar 26, 2025 · 5 comments
Open

Warmup connections #1982

frussian opened this issue Mar 26, 2025 · 5 comments

Comments

@frussian
Copy link

Is there a proper way to warm up a connection using HostClient? There's an AcquireConn method but it may return a ready connection from pool whereas I need to establish and save a new connection to the connection pool. Also, I need to warm up connections in the middle of using HostClient so AcquireConn doesn't sound like a proper solution due to above reason.

@erikdubbelboer
Copy link
Collaborator

What exactly do you mean with warmup?

@frussian
Copy link
Author

Let's say we have an SLA of 250ms, which will be our timeout for an HTTP request. For example, we know that we establish a TCP connection in ~200ms and the HTTP request/response roundtrip takes ~100ms, therefore, we will timeout always because 200ms+100ms > 250ms. We want to have an interface to warmup or, in other words, create a number of TCP connections in advance before the actual requests or even in parallel, and save them to the connection pool to overcome the timeout problem. Currently, there's no such interface. We have an AcquireConn but again, it may return an already established connection from the pool.

@erikdubbelboer
Copy link
Collaborator

AcquireConn only returns an already established connection if you call ReleaseConn to release it. So you could do something like this:

func main() {
	hc := fasthttp.HostClient{
		Addr: "google.com:80",
	}

	// Use a closure so we can defer ReleaseConn. The return value of
	// AcquireConn is not exported so we can't easily store it in a slice
	// to release it later for example.
	func() {
		for range 10 {
			c, err := hc.AcquireConn(0, false)
			if err != nil {
				panic(err)
			}

			defer hc.ReleaseConn(c)
		}
	}()

	// At this point we will have 10 connections open ready for use.
	fmt.Println(hc.ConnsCount())
}

@frussian
Copy link
Author

frussian commented Apr 28, 2025

Yes, but we may need to do a warmup dynamically while we are already using HostClient. So AcquireConn may return an existing connection instead of creating a new one.

@erikdubbelboer
Copy link
Collaborator

Does that matter? As long as you aquire as many conns as you need it should be fine. And it's a quick process so the conn can be used again right away.

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

2 participants