Netty代码写法总结2Java
文章发布较早,内容可能过时,阅读注意甄别。
# 总结2
# 构造上下文
DNS解析,Map转Set
private final Set<Future<DnsResponse>> queriesInProgress =
Collections.newSetFromMap(new IdentityHashMap<Future<DnsResponse>, Boolean>());
private List<InetAddress> resolvedAddresses;
private StringBuilder trace;
private int allowedQueries;
private boolean triedCNAME;
DnsNameResolverContext(DnsNameResolver parent, String hostname, int port, Promise<InetSocketAddress> promise) {
this.parent = parent;
this.promise = promise;
this.hostname = hostname;
this.port = port;
maxAllowedQueries = parent.maxQueriesPerResolve();
resolveAddressTypes = parent.resolveAddressTypesUnsafe();
allowedQueries = maxAllowedQueries;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 增加链路StringBuilder
private void addTrace(InetSocketAddress nameServerAddr, String msg) {
if (trace == null) {
trace = new StringBuilder(128);
}
trace.append(StringUtil.NEWLINE);
trace.append("\tfrom ");
trace.append(nameServerAddr);
trace.append(": ");
trace.append(msg);
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 定义组Group的概念
/**
* A {@link NameResolverGroup} of {@link DnsNameResolver}s.
*/
public final class DnsNameResolverGroup extends NameResolverGroup<InetSocketAddress> {
private final ChannelFactory<? extends DatagramChannel> channelFactory;
private final InetSocketAddress localAddress;
private final Iterable<InetSocketAddress> nameServerAddresses;
public DnsNameResolverGroup(
Class<? extends DatagramChannel> channelType,
InetSocketAddress nameServerAddress) {
this(channelType, ANY_LOCAL_ADDR, nameServerAddress);
}
/**
* Creates and manages {@link NameResolver}s so that each {@link EventExecutor} has its own resolver instance.
*/
public abstract class NameResolverGroup<T extends SocketAddress> implements Closeable {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 移位操作及for(;;)死循环
private int allocateId() {
int id = ThreadLocalRandom.current().nextInt(parent.promises.length());
final int maxTries = parent.promises.length() << 1;
int tries = 0;
for (;;) {
if (parent.promises.compareAndSet(id, null, this)) {
return id;
}
id = id + 1 & 0xFFFF;
if (++ tries >= maxTries) {
throw new IllegalStateException("query ID space exhausted: " + question);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17