Skip to content

Instantly share code, notes, and snippets.

View methane's full-sized avatar

Inada Naoki methane

View GitHub Profile
@methane
methane / 1.readme.md
Last active August 19, 2026 09:36
Load balancing via reverse proxy, rather than SO_REUSEPORT

Why a Reverse Proxy Can Beat SO_REUSEPORT on Tail Latency

Summary

When several server workers listen on one port with SO_REUSEPORT, the kernel normally assigns each new TCP connection using a hash. This is fast and scalable, but the hash does not know which worker is busy.

A reverse proxy can expose each worker as a separate upstream endpoint and send new work to the worker with the fewest active connections (least_conn). Because this decision uses current load, it can reduce accidental queues and improve p99 response time.

This does not mean a reverse proxy is always faster. The expected benefit is mainly lower tail latency under concurrency, bursts, or uneven request durations.

diff --git a/granian/server/__init__.py b/granian/server/__init__.py
index f04daa9..61f35c8 100644
--- a/granian/server/__init__.py
+++ b/granian/server/__init__.py
@@ -1,5 +1,6 @@
from .._granian import BUILD_GIL
from .mp import MPServer as MPServer
+from .mp import MultiPortServer as MultiPortServer
from .mt import MTServer as MTServer
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "niquests>=3.18.2",
# ]
# ///
import niquests
from niquests.adapters import HTTPAdapter
import threading
import time
FATAL(RSC-016): build/epub/Python.epub/genindex-A.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced, but not declared.
FATAL(RSC-016): build/epub/Python.epub/genindex-B.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced, but not declared.
FATAL(RSC-016): build/epub/Python.epub/genindex-C.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced, but not declared.
FATAL(RSC-016): build/epub/Python.epub/genindex-D.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced, but not declared.
FATAL(RSC-016): build/epub/Python.epub/genindex-E.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced, but not declared.
FATAL(RSC-016): build/epub/Python.epub/genindex-F.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced, but not declared.
FATAL(RSC-016): build/epub/Python.epub/genindex-G.xhtml(34,29): Fatal Error while parsing file: The entity "ndash" was referenced,
@methane
methane / f.py
Last active April 10, 2024 13:50
short-traceback
import sys
import os
import io
import traceback
import json
import pathlib
import sub
try:
Run uwsgi
uwsgi --wsgi-file=mt_sample.py --threads 8 --workers 4 --max-requests 40 --http-socket=:8000 --master --min-worker-lifetime=7 -L
Run hey[1]
hey -c 32 -z 10m 'http://127.0.0.1:8000/'
[1] hey: https://github.com/rakyll/hey
@methane
methane / mariadb_float.md
Last active February 26, 2024 13:25
mariadb float type
mysql> create table t (id integer primary key, f1 float, f2 float(10,4));

mysql> insert into t (id, f1, f2) values (1, 0.0001, 0.0001);

mysql> select f1, f1=0.0001, f1=0.0001e0, f2, f2=0.0001, f2=0.0001e0 from t;
+--------+-----------+-------------+--------+-----------+-------------+
| f1     | f1=0.0001 | f1=0.0001e0 | f2     | f2=0.0001 | f2=0.0001e0 |
+--------+-----------+-------------+--------+-----------+-------------+
| 0.0001 | 0 | 0 | 0.0001 | 1 | 0 |
@methane
methane / bench_indent.py
Last active July 29, 2023 02:05
bench_indent.py
# https://github.com/python/cpython/pull/107374
import sys
import textwrap
import timeit
filename = "Objects/unicodeobject.c"
if len(sys.argv) > 1:
filename = sys.argv[1]
diff -r ffdfb1066ac6 contrib/win32/hg.bat
--- a/contrib/win32/hg.bat Thu Mar 02 15:34:45 2023 +0100
+++ b/contrib/win32/hg.bat Wed Jun 28 18:53:44 2023 +0900
@@ -5,6 +5,7 @@
set HG=%~f0
set PYTHONLEGACYWINDOWSSTDIO=1
+set PYTHONLEGACYWINDOWSFSENCODING=1
rem Use a full path to Python (relative to this script) if it exists,
@methane
methane / bench_dict.py
Last active May 16, 2022 11:09
dict benchmark
#!/usr/bin/env python3
import pyperf
def build_dict_str(n):
mydict = {str(k): k for k in range(n)}
return mydict, list(mydict)
def build_dict_int(n):