All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.connection.dns.GroupAddressResolver Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/**
 * Copyright 2018 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson.connection.dns;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import io.netty.resolver.AddressResolver;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.Promise;

/**
 * Workaround for https://github.com/netty/netty/issues/8261
 * 
 * @author Nikita Koksharov
 *
 */
class GroupAddressResolver implements AddressResolver {

    private final List> resolvers;
    
    public GroupAddressResolver(List> resolvers) {
        super();
        this.resolvers = resolvers;
    }

    @Override
    public boolean isSupported(SocketAddress address) {
        for (AddressResolver addressResolver : resolvers) {
            if (addressResolver.isSupported(address)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isResolved(SocketAddress address) {
        for (AddressResolver addressResolver : resolvers) {
            if (addressResolver.isResolved(address)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Future resolve(SocketAddress address) {
        final Promise promise = ImmediateEventExecutor.INSTANCE.newPromise();
        final AtomicInteger counter = new AtomicInteger(resolvers.size());
        for (AddressResolver addressResolver : resolvers) {
            addressResolver.resolve(address).addListener(new FutureListener() {
                @Override
                public void operationComplete(Future future) throws Exception {
                    if (future.isSuccess()) {
                        promise.trySuccess(future.getNow());
                    }
                    
                    if (counter.decrementAndGet() == 0) {
                        if (!future.isSuccess()) {
                            promise.tryFailure(future.cause());
                        }
                    }
                }
            });
        }
        return promise;
    }

    @Override
    public Future resolve(SocketAddress address, Promise promise) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Future> resolveAll(SocketAddress address) {
        final Promise> promise = ImmediateEventExecutor.INSTANCE.newPromise();
        final AtomicInteger counter = new AtomicInteger(resolvers.size());
        for (AddressResolver addressResolver : resolvers) {
            addressResolver.resolveAll(address).addListener(new FutureListener>() {
                @Override
                public void operationComplete(Future> future) throws Exception {
                    if (future.isSuccess()) {
                        promise.trySuccess(future.getNow());
                    }
                    
                    if (counter.decrementAndGet() == 0) {
                        if (!future.isSuccess()) {
                            promise.tryFailure(future.cause());
                        }
                    }
                }
            });
        }
        return promise;
    }

    @Override
    public Future> resolveAll(SocketAddress address, Promise> promise) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void close() {
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy