cloudflare / pingora

A library for building fast, reliable and evolvable network services.
Apache License 2.0
20.3k stars 1.1k forks source link

implement zerocopy read/write on Stream with splice #160

Open xujihui1985 opened 3 months ago

xujihui1985 commented 3 months ago

What is the problem your feature solves, or the need it fulfills?

I would like to forward traffic from downstream to upstream in an L4 proxy using splice to avoid multiple user-space memory copies. However, I've found it difficult to implement this using the Stream object. Is there any way to achieve this?

Describe the solution you'd like

maybe expose the underlying tcpstream from the Stream object?, appreciate if someone can shed some light on it

eaufavor commented 3 months ago

For now pingora-proxy is a layer 7 proxy. That means it decrypts and parses the request and response. It also needs to handle connection management (keepalive).

Here is an example of a L4 proxy with the stream exposed.

xujihui1985 commented 3 months ago

@eaufavor Thank you for your response. Yes, I've experimented with the Layer 4 (L4) proxy example. In developing a relay server, I attempted to eliminate memcpy usage by employing splice for downstream and upstream data flow. To accomplish this, I resorted to a workaround that involved downcasting a Box<dyn IO> to its underlying Stream and add couple of method in the l4::Stream in order to get the underlying tcpstream, which evidently isn't an ideal approach. Do you believe supporting stream splicing is a viable direction for Pingora to consider?

eaufavor commented 3 months ago

If we ever build a pure L4 relay we will definitely take that into consideration.

xujihui1985 commented 3 months ago

If we ever build a pure L4 relay we will definitely take that into consideration.

thanks, let me know one day you decide to this direction, I'm happy to contribute. cheers