|
15 | 15 | */ |
16 | 16 | package io.reactivesocket; |
17 | 17 |
|
18 | | -import io.reactivesocket.internal.rx.EmptySubscription; |
19 | 18 | import org.reactivestreams.Publisher; |
20 | | -import org.reactivestreams.Subscriber; |
21 | | -import org.reactivestreams.Subscription; |
22 | | - |
23 | | -import java.util.NoSuchElementException; |
24 | | -import java.util.concurrent.CompletableFuture; |
25 | | -import java.util.concurrent.ScheduledExecutorService; |
26 | | -import java.util.concurrent.TimeUnit; |
27 | | -import java.util.concurrent.TimeoutException; |
28 | | -import java.util.concurrent.atomic.AtomicBoolean; |
29 | | - |
30 | | -@FunctionalInterface |
31 | | -public interface ReactiveSocketFactory<T, R extends ReactiveSocket> { |
32 | | - |
33 | | - Publisher<R> call(T t); |
| 19 | +import java.net.SocketAddress; |
34 | 20 |
|
| 21 | +/** |
| 22 | + * Factory of ReactiveSocket interface |
| 23 | + * This abstraction is useful for abstracting the creation of a ReactiveSocket |
| 24 | + * (e.g. inside the LoadBalancer which create ReactiveSocket as needed) |
| 25 | + */ |
| 26 | +public interface ReactiveSocketFactory<T> { |
35 | 27 | /** |
36 | | - * Gets a socket in a blocking manner |
37 | | - * @param t configuration to create the reactive socket |
38 | | - * @return blocks on create the socket |
| 28 | + * Construct the ReactiveSocket |
| 29 | + * @return |
39 | 30 | */ |
40 | | - default R callAndWait(T t) { |
41 | | - CompletableFuture<R> future = new CompletableFuture<>(); |
42 | | - |
43 | | - call(t) |
44 | | - .subscribe(new Subscriber<R>() { |
45 | | - @Override |
46 | | - public void onSubscribe(Subscription s) { |
47 | | - s.request(1); |
48 | | - } |
49 | | - |
50 | | - @Override |
51 | | - public void onNext(R reactiveSocket) { |
52 | | - future.complete(reactiveSocket); |
53 | | - } |
54 | | - |
55 | | - @Override |
56 | | - public void onError(Throwable t) { |
57 | | - future.completeExceptionally(t); |
58 | | - } |
59 | | - |
60 | | - @Override |
61 | | - public void onComplete() { |
62 | | - future.completeExceptionally(new NoSuchElementException("Sequence contains no elements")); |
63 | | - } |
64 | | - }); |
65 | | - |
66 | | - return future.join(); |
67 | | - } |
| 31 | + Publisher<ReactiveSocket> apply(); |
68 | 32 |
|
69 | 33 | /** |
70 | | - * |
71 | | - * @param t the configuration used to create the reactive socket |
72 | | - * @param timeout timeout |
73 | | - * @param timeUnit timeout units |
74 | | - * @param executorService ScheduledExecutorService to schedule the timeout on |
75 | | - * @return |
| 34 | + * @return a positive numbers representing the availability of the factory. |
| 35 | + * Higher is better, 0.0 means not available |
76 | 36 | */ |
77 | | - default Publisher<R> call(T t, long timeout, TimeUnit timeUnit, ScheduledExecutorService executorService) { |
78 | | - Publisher<R> reactiveSocketPublisher = subscriber -> { |
79 | | - AtomicBoolean complete = new AtomicBoolean(); |
80 | | - subscriber.onSubscribe(EmptySubscription.INSTANCE); |
81 | | - call(t) |
82 | | - .subscribe(new Subscriber<R>() { |
83 | | - @Override |
84 | | - public void onSubscribe(Subscription s) { |
85 | | - s.request(1); |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - public void onNext(R reactiveSocket) { |
90 | | - subscriber.onNext(reactiveSocket); |
91 | | - } |
92 | | - |
93 | | - @Override |
94 | | - public void onError(Throwable t) { |
95 | | - subscriber.onError(t); |
96 | | - } |
97 | | - |
98 | | - @Override |
99 | | - public void onComplete() { |
100 | | - if (complete.compareAndSet(false, true)) { |
101 | | - subscriber.onComplete(); |
102 | | - } |
103 | | - } |
104 | | - }); |
105 | | - |
106 | | - executorService.schedule(() -> { |
107 | | - if (complete.compareAndSet(false, true)) { |
108 | | - subscriber.onError(new TimeoutException()); |
109 | | - } |
110 | | - }, timeout, timeUnit); |
111 | | - }; |
112 | | - |
113 | | - return reactiveSocketPublisher; |
114 | | - } |
| 37 | + double availability(); |
115 | 38 |
|
| 39 | + /** |
| 40 | + * @return an identifier of the remote location |
| 41 | + */ |
| 42 | + T remote(); |
116 | 43 | } |
0 commit comments