public inbox for gwml@vger.gnuweeb.org
 help / color / mirror / Atom feed
From: Ammar Faizi <ammarfaizi2@gnuweeb.org>
To: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Cc: Ahmad Gani <reyuki@gnuweeb.org>,
	GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: Re: [PATCH gwproxy v8 2/2] gwproxy: refactor code base to add experimental raw DNS backend
Date: Sun, 7 Sep 2025 13:22:49 +0900	[thread overview]
Message-ID: <aL0ImQBGb3b4Md4y@biznet-home.integral.gnuweeb.org> (raw)
In-Reply-To: <CAOG64qOL4Wi_Nbq-gMYFKDSUdiqVyb2ZxN3tuiYQpt=JtmtpgQ@mail.gmail.com>

On Sat, Sep 06, 2025 at 08:30:54PM +0700, Alviro Iskandar Setiawan wrote:
> Let me know your design if you have one too.

I have a small proposal. Writing this email from my Android phone right
now. I am still traveling. Sitting and waiting are boring, so I decided
to exercise my brain designing this solution. It keeps my brain active.

Have these two structs:

struct stack_u16 {
        uint16_t sp;
        uint16_t bp;
        uint16_t *arr;
};

struct dns_resolver {
        struct stack_u16 stack;
        struct gwp_conn_pair **sess_map;
        uint16_t sess_map_cap;
};

[ The struct dns_resolver MAY also cover socket, addr, etc. Now my
  primary point here is about the session mapping data structure. ]

1) @stack is used to keep track of unused indexes in @sess_map.

   - Push all unused indexes into the @stack, the index will be used as
     the DNS query txid.

   - When creating a DNS query, pop the stack, use the popped number as
     the txid. The txid is also used to store the corresponding
     gwp_conn_pair session (sess_map[txid] = ptr to conn session).

   - When the sess_map[txid] is no longer used, set
     (sess_map[txid] = NULL) and push the txid back into the @stack.

2) @sess_map_cap is used to keep track of the allocated size of
   @sess_map. Don't allocate 65536 at once to keep the memory footprint
   low when the proxy server is not fully utilized.

   - Start allocating @sess_map from size 16. Push 15, 14, 13, ..., 0
     into the @stack.

   - Double the size when the slot is exhausted. When double-ing the
     size, the new allocated indexes are all pushed into the @stack.
     The slot exhaustion happens when the @stack is empty, it can be
     identified at the pop operation.

   - @sess_map array can be expanded up to 65536.

   - @sess_map MAY only be shrunk back to 16 when all members in
     @sess_map are NULL. When to shrink the array can be identified at
     the push operation. If the number of elements in the @stack is
     equal to @sess_map_cap, that means @sess_map is 100% free and can
     be reset to the initial state (back to size 16). The content of
     the @stack MUST also be reset at the shrink operation to keep
     the next pop and push operations in sync.

3) Handle more than 65535 clients in a single thread? Doable!

   Just have more than one struct dns_resolver per thread, create a new
   DNS resolver in the same thread once the 65535 limit is hit.

      Client     0 to 65535,  use DNS resolver A.
      Client 65536 to 131071, use DNS resolver B (new UDP socket!).
      and so on...

   I doubt we will reach that limit. I think it is better to add more
   threads to utilize more CPU cores than keep adding more clients in
   a single thread. But we should still support it.

-- 
Ammar Faizi


  parent reply	other threads:[~2025-09-07  4:23 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-29  7:55 [PATCH gwproxy v8 0/2] Initial work on integration of DNS parser lib in gwproxy Ahmad Gani
2025-08-29  7:55 ` [PATCH gwproxy v8 1/2] dnsparser: Add dns parser code Ahmad Gani
2025-08-29  7:55 ` [PATCH gwproxy v8 2/2] gwproxy: refactor code base to add experimental raw DNS backend Ahmad Gani
2025-09-05 16:26   ` Alviro Iskandar Setiawan
2025-09-06  4:32     ` Ahmad Gani
2025-09-06  5:16       ` Ahmad Gani
2025-09-06  6:17       ` Ahmad Gani
2025-09-06  6:48         ` Ahmad Gani
2025-09-06  7:02           ` Alviro Iskandar Setiawan
2025-09-06  6:47       ` Alviro Iskandar Setiawan
2025-09-09  2:38         ` reyuki
2025-09-06 11:27       ` Ahmad Gani
2025-09-06 12:01         ` Alviro Iskandar Setiawan
2025-09-06 12:58         ` Ahmad Gani
2025-09-06 13:30           ` Alviro Iskandar Setiawan
2025-09-06 13:44             ` Alviro Iskandar Setiawan
2025-09-06 14:26             ` Ahmad Gani
2025-09-06 14:30               ` Alviro Iskandar Setiawan
2025-09-07  4:22             ` Ammar Faizi [this message]
2025-09-07  5:57               ` Ammar Faizi
2025-09-07  6:39               ` Ahmad Gani
2025-09-07  6:40                 ` Ahmad Gani
2025-09-07  6:43                   ` Ammar Faizi
2025-09-07  7:06                 ` Ammar Faizi
2025-09-07  7:17                   ` Ahmad Gani
2025-09-07  9:52               ` Ahmad Gani
2025-09-07 10:19                 ` Ammar Faizi
2025-09-07 10:36                   ` Ahmad Gani
2025-09-06  7:14     ` Alviro Iskandar Setiawan
2025-09-06  7:21       ` Ahmad Gani
2025-09-06  7:47   ` Alviro Iskandar Setiawan
2025-09-06 11:01     ` Ahmad Gani
2025-09-05  9:18 ` [PATCH gwproxy v8 0/2] Initial work on integration of DNS parser lib in gwproxy Ammar Faizi
2025-09-05  9:34   ` Alviro Iskandar Setiawan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aL0ImQBGb3b4Md4y@biznet-home.integral.gnuweeb.org \
    --to=ammarfaizi2@gnuweeb.org \
    --cc=alviro.iskandar@gnuweeb.org \
    --cc=gwml@vger.gnuweeb.org \
    --cc=reyuki@gnuweeb.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox