gRPC-Java Passive update NameResolver

32 views
Skip to first unread message

Linh Chu Nguyen

unread,
Jul 15, 2025, 12:48:30 AMJul 15
to grpc.io
Hi gRPC team,
Firstly, thanks for listen to me. I have a question that in my current project, I already have a daemon service to check Name server (DNS) and when have a changed, I want it call custom NameResolver to update gRPC client channel. I have a solution like it:

public class ExampleNameResolver extends NameResolver {
    private Listener2 listener;
    private final String serviceName;

    private final AtomicReference<List<InetSocketAddress>> currentAddresses = new AtomicReference<>(Collections.emptyList());

    public ExampleNameResolver(URI targetUri) {
        this.serviceName = targetUri.getPath().substring(1);
    }

    @Override
    public String getServiceAuthority() {
        return "example-authority";
    }

    @Override
    public void start(Listener2 listener) {
        this.listener = listener;
        if (!currentAddresses.get().isEmpty()) {
            updateResolution();
        }
    }

    @Override
    public void refresh() {

    }

    @Override
    public void shutdown() {

    }

    public void updateAddresses(List<InetSocketAddress> newAddresses) {
        currentAddresses.set(newAddresses);
        updateResolution();
    }

    private void updateResolution() {
        if (listener == null) {
            return;
        }
        List<EquivalentAddressGroup> groups = currentAddresses.get().stream()
            .map(EquivalentAddressGroup::new)
            .collect(Collectors.toList());

        ResolutionResult result = ResolutionResult.newBuilder()
            .setAddresses(groups)
            .build();

        listener.onResult(result);
    }
}

Do you think about this solution? Have any risk? Do you have any suggestion?
(I mean about the logical, NameResolver will be auto call to get newest Name server (proactive), but I want it only update when I want (passive)
Thank and best regards team.
Reply all
Reply to author
Forward
0 new messages