/
LoadBalancerFactory.java
83 lines (77 loc) · 4.15 KB
/
LoadBalancerFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* Copyright © 2018 Apple Inc. and the ServiceTalk project authors
*
* 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 io.servicetalk.client.api;
import io.servicetalk.concurrent.api.Publisher;
import io.servicetalk.transport.api.ExecutionStrategy;
import io.servicetalk.transport.api.ExecutionStrategyInfluencer;
import java.util.Collection;
import java.util.Collections;
/**
* A factory for creating {@link LoadBalancer} instances.
*
* @param <ResolvedAddress> The type of address after resolution.
* @param <C> The type of connection.
*/
@FunctionalInterface
public interface LoadBalancerFactory<ResolvedAddress, C extends LoadBalancedConnection> extends
ExecutionStrategyInfluencer<ExecutionStrategy> {
/**
* Create a new {@link LoadBalancer}.
*
* @param eventPublisher A stream of {@link ServiceDiscovererEvent}s which the {@link LoadBalancer} can use to
* connect to physical hosts. Typically generated from a
* {@link ServiceDiscoverer#discover(Object) ServiceDiscoverer}.
* @param connectionFactory {@link ConnectionFactory} that the returned {@link LoadBalancer} will use to generate
* new connections. Returned {@link LoadBalancer} will own the responsibility for this {@link ConnectionFactory}
* and hence will call {@link ConnectionFactory#closeAsync()} when {@link LoadBalancer#closeAsync()} is called.
* @param <T> Type of connections created by the passed {@link ConnectionFactory}.
* @return a new {@link LoadBalancer}.
* @deprecated In the future only {@link #newLoadBalancer(String, Publisher, ConnectionFactory)} will remain,
* please use that method instead.
*/
@Deprecated // FIXME: 0.43 - remove deprecated method
default <T extends C> LoadBalancer<T> newLoadBalancer(
Publisher<? extends ServiceDiscovererEvent<ResolvedAddress>> eventPublisher,
ConnectionFactory<ResolvedAddress, T> connectionFactory) {
return newLoadBalancer("UNKNOWN",
eventPublisher.map(Collections::singletonList), connectionFactory);
}
/**
* Create a new {@link LoadBalancer}.
*
* @param targetResource A {@link String} representation of the target resource for which the created instance
* will perform load balancing. Bear in mind, load balancing is performed over the a collection of hosts provided
* via the {@code eventPublisher} which may not correspond directly to a single unresolved address, but potentially
* a merged collection.
* @param eventPublisher A stream of {@link Collection}<{@link ServiceDiscovererEvent}>
* which the {@link LoadBalancer} can use to connect to physical hosts. Typically generated
* from {@link ServiceDiscoverer#discover(Object) ServiceDiscoverer}.
* @param connectionFactory {@link ConnectionFactory} that the returned {@link LoadBalancer} will use to generate
* new connections. Returned {@link LoadBalancer} will own the responsibility for this {@link ConnectionFactory}
* and hence will call {@link ConnectionFactory#closeAsync()} when {@link LoadBalancer#closeAsync()} is called.
* @param <T> Type of connections created by the passed {@link ConnectionFactory}.
* @return a new {@link LoadBalancer}.
*/
<T extends C> LoadBalancer<T> newLoadBalancer(
String targetResource,
Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
ConnectionFactory<ResolvedAddress, T> connectionFactory);
@Override
default ExecutionStrategy requiredOffloads() {
// safe default--implementations are expected to override
return ExecutionStrategy.offloadAll();
}
}