Skip to content

Draft Feature: Convert APIs using &str for addresses to ToSocketAddr #804

@tarka

Description

@tarka

Currently Pingora clients specify TCP addresses add &str which are converted internally. However Rust std already has types for addresses/sockets and their conversion i.e. SocketAddr and ToSocketAddrs. Most importantly, it already has blanket implementations for a number of conversions, including &str.

I propose converting all uses of &str with ToSocketAddrs where a SocketAddr would be appropriate, and performs conversion internally. Note that we do not use IpAddr as it does not contain all required information for IPv6 (specifically scope_id for link-local addresses).

As ToSocketAddrs is already implemented for &str this is not a breaking API change; existing API calls providing &str addresses will be automatically converted. However it would be useful to modify some APIs that currently have no return type (e.g. add_tls(addr)) to return a Result. There may also be the option for e.g. add_tls() to be provided with multiple addresses, that that is out of scope here.

I have already have a draft branch for this work (I thought it was going to be necessary for a project but managed to work around it; see below).

Use case

In Vicarian I allow the admin to specify listening on an interface e.g. eth0. The provided interface is expanded to all addresses using getifaddrs() with returns a SocketAddr-like list. These are converted into SocketAddr to preserve information (scope_id), but need to be serialised to &str for Pingora, which then converts them back. This is all a bit redundant and easy to get wrong (just serialising the IP portion of an IPv6 address will break with link-local addresses). Providing ToSocketAddr-compatible types would be more idiomatic Rust.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ergonomicsEase of use, developer friendliness

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions