cloudflare / pingora

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

Add `Session::digest_mut()` method #270

Closed palant closed 1 day ago

palant commented 3 weeks ago

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

I want to anonymize client IP addresses, so that IP address 1.2.3.4 shows up as 1.2.3.0 in the logs for example. I can adjust the log output code of course, but ideally the client IP address would be adjusted at Session level so that whatever code might decide to store or log an IP address will always get the anonymized one.

Describe the solution you'd like

As Session::client_addr goes to Session::digest internally, that’s what will need to be changed. This can be done via a Session::digest_mut method granting mutable access to the digest.

Describe alternatives you've considered

I already have a Session wrapper, and I could use it to override Session::client_addr return value (in fact, that’s what I am going to implement now). However, some code might still unintentionally access the unwrapped Session instance or get the IP address via Session::digest which is much harder to override.