Rate limiting example?

I need to limit how often I’m making requests using libsoup 3 and I’m curious if there is any documentation or examples doing so.

I see the max-conns property and that’s kind of there, but the services I need to access are more worried about how many request per second.

It looks like I might be able to do something with Soup.SessionFeature but I’m not seeing the public API to be able to intercept Soup.Session.send* so any help or pointers would be appreciated.

This solution might not be libsoup3 specific, but I would usually create a thread pool or use a latch to restrict the number of currently active requests to a maximum number. The maximum number of concurrent requests n must be smaller than the rate limit L times the average request duration d:

n <= L * d

To avoid hitting the rate limit, you could also plan a small safety buffer.

Alternatively, you tackle the problem optimistically and implement a backoff mechanism, i.e., when you hit the rate limit, wait some random time (each request another random time) and retry. When you still hit the rate limit, wait a little longer (but still random time) and retry.

I’m looking for libsoup specific, was that not clear in my original post?

I’m trying to figure out if I can hook into soup_session_send* and defer it or do I need to write a wrapper to drop things in an AsyncQueue and then have a timeout call the soup_send family of functions.

It seems like Soup.SessionFeature might be able to do this, but like I said, I haven’t found anything in the Soup API that would allow me to defer/delay sending a message and wondering if I’m missing anything there.