Home / File/ DnsQueryIdSpace.java — netty Source File

DnsQueryIdSpace.java — netty Source File

Architecture documentation for DnsQueryIdSpace.java, a java file in the netty codebase.

Entity Profile

Relationship Graph

Source Code

/*
 * Copyright 2024 The Netty Project
 *
 * The Netty Project licenses this file to you 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:
 *
 *   https://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.netty.resolver.dns;

import io.netty.util.internal.MathUtil;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * Special data-structure that will allow to retrieve the next query id to use, while still guarantee some sort
 * of randomness.
 * The query id will be between 0 (inclusive) and 65535 (inclusive) as defined by the RFC.
 */
final class DnsQueryIdSpace {
    private static final int MAX_ID = 65535;
    private static final int BUCKETS = 4;
    // Each bucket is 16kb of size.
    private static final int BUCKET_SIZE = (MAX_ID + 1) / BUCKETS;

    // If there are other buckets left that have at least 500 usable ids we will drop an unused bucket.
    private static final int BUCKET_DROP_THRESHOLD = 500;
    private final DnsQueryIdRange[] idBuckets = new DnsQueryIdRange[BUCKETS];

    DnsQueryIdSpace() {
        assert idBuckets.length == MathUtil.findNextPositivePowerOfTwo(idBuckets.length);
        // We start with 1 bucket.
        idBuckets[0] = newBucket(0);
    }

    private static DnsQueryIdRange newBucket(int idBucketsIdx) {
        return new DnsQueryIdRange(BUCKET_SIZE, idBucketsIdx * BUCKET_SIZE);
    }

    /**
     * Returns the next ID to use for a query or {@code -1} if there is none left to use.
     *
     * @return next id to use.
     */
    int nextId() {
        int freeIdx = -1;
        for (int bucketIdx = 0; bucketIdx < idBuckets.length; bucketIdx++) {
            DnsQueryIdRange bucket = idBuckets[bucketIdx];
            if (bucket != null) {
                int id = bucket.nextId();
                if (id != -1) {
                    return id;
// ... (153 more lines)

Subdomains

Frequently Asked Questions

What does DnsQueryIdSpace.java do?
DnsQueryIdSpace.java is a source file in the netty codebase, written in java. It belongs to the NativeResolver domain, JavaResolver subdomain.
Where is DnsQueryIdSpace.java in the architecture?
DnsQueryIdSpace.java is located at resolver-dns/src/main/java/io/netty/resolver/dns/DnsQueryIdSpace.java (domain: NativeResolver, subdomain: JavaResolver, directory: resolver-dns/src/main/java/io/netty/resolver/dns).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free