blob: a34371383d14fdbad95f6447816128110cf1112f (
plain) (
blame)
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.network.rpc;
import com.yahoo.jrt.slobrok.api.IMirror;
import com.yahoo.jrt.slobrok.api.Mirror;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class NamedRPCService implements RPCService {
private final IMirror mirror;
private final String pattern;
private int addressIdx = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
private int addressGen = 0;
private List<Mirror.Entry> addressList = null;
/**
* Create a new RPCService backed by the given network and using the given service pattern.
*
* @param mirror The naming server to send queries to.
* @param pattern The pattern to use when querying.
*/
public NamedRPCService(IMirror mirror, String pattern) {
this.mirror = mirror;
this.pattern = pattern;
}
/**
* Resolve a concrete address from this service. This service may represent multiple remote sessions, so this will
* select one that is online.
*
* @return A concrete service address.
*/
public synchronized RPCServiceAddress resolve() {
if (addressGen != mirror.updates()) {
addressGen = mirror.updates();
addressList = mirror.lookup(pattern);
}
if (addressList != null && !addressList.isEmpty()) {
++addressIdx;
if (addressIdx >= addressList.size()) {
addressIdx = 0;
}
Mirror.Entry entry = addressList.get(addressIdx);
return new RPCServiceAddress(entry.getName(), entry.getSpec());
}
return null;
}
}
|