Linux operatsioonisüsteemis töötab võrguühendus: erinevus redaktsioonide vahel
Allikas: Imre kasutab arvutit
Mine navigeerimisribaleMine otsikasti
(Uus lehekülg: '===Sissejuhatus=== Osutub, et tcp/ip network stack Linux, Windows, BSD jt implementatsioonid võimaldavad teatud tingimustel mitmel protsessil korraga sama pordi kasutada. ===Kasulikud lisamaterjalid=== * TODO') |
Resümee puudub |
||
| 1. rida: | 1. rida: | ||
===Sissejuhatus=== |
===Sissejuhatus=== |
||
| − | Osutub, et tcp/ip network stack Linux, Windows, BSD jt implementatsioonid võimaldavad teatud tingimustel mitmel protsessil korraga sama pordi kasutada. |
+ | Osutub, et tcp/ip network stack Linux, Windows, BSD jt implementatsioonid võimaldavad teatud tingimustel mitmel protsessil korraga sama pordi kasutada. Seda võimalust kehastab 'so_reuseport' socket option. |
| + | |||
| + | ===Tööpõhimõte=== |
||
| + | |||
| + | TODO |
||
| + | |||
| + | Väited |
||
| + | |||
| + | * haproxy - kasutatakse vaikimisi |
||
| + | * nginx - vaikimisi ei kasutata, aga soovitatakse kasutada jõudluse suurendamise eesmärgil |
||
| + | * zabbix-agent2 - ei ole võimalik kasutada |
||
| + | |||
| + | ===Kasutamine=== |
||
| + | |||
| + | ====Python skript==== |
||
| + | |||
| + | <pre> |
||
| + | # cat socket-01-server.py |
||
| + | import socket |
||
| + | import os |
||
| + | import sys |
||
| + | |||
| + | PORT = 8083 |
||
| + | # Fetch the Process ID so we can easily tell which instance answers the client |
||
| + | PID = os.getpid() |
||
| + | |||
| + | # 1. Create a standard TCP IPv4 Socket |
||
| + | server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
||
| + | |||
| + | # 2. THE MAGIC OPTIONS: Enable Reuse Address and Reuse Port |
||
| + | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
||
| + | server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) |
||
| + | |||
| + | # 3. Bind and Listen (Standard blocking mode) |
||
| + | server_socket.bind(('0.0.0.0', PORT)) |
||
| + | server_socket.listen(10) |
||
| + | |||
| + | print(f"[Worker PID: {PID}] Multi-core listening gate opened on port {PORT}...") |
||
| + | |||
| + | try: |
||
| + | while True: |
||
| + | # This call BLOCKS completely. The script sleeps right here in the kernel |
||
| + | # until the Linux 4-tuple hash decides to route a client to THIS specific socket instance. |
||
| + | client_socket, client_address = server_socket.accept() |
||
| + | |||
| + | # Read the raw incoming HTTP request payload (up to 4KB) |
||
| + | request = client_socket.recv(4096) |
||
| + | |||
| + | if request: |
||
| + | # Construct a basic HTTP response containing the answering process ID |
||
| + | body = f"Hello from Worker Process ID: {PID}\n" |
||
| + | response = ( |
||
| + | "HTTP/1.1 200 OK\r\n" |
||
| + | "Content-Type: text/plain\r\n" |
||
| + | f"Content-Length: {len(body)}\r\n" |
||
| + | "Connection: close\r\n" |
||
| + | "\r\n" |
||
| + | f"{body}" |
||
| + | ).encode('utf-8') |
||
| + | |||
| + | # Fire the response back to the client |
||
| + | client_socket.sendall(response) |
||
| + | |||
| + | # Close the socket immediately to return the client line |
||
| + | client_socket.close() |
||
| + | |||
| + | except KeyboardInterrupt: |
||
| + | print(f"\n[Worker {PID}] Shutting down down gracefully.") |
||
| + | finally: |
||
| + | server_socket.close() |
||
| + | </pre> |
||
| + | |||
| + | ====Nginx==== |
||
| + | |||
| + | TODO |
||
| + | |||
| + | ====haproxy==== |
||
| + | |||
| + | TODO |
||
| + | |||
===Kasulikud lisamaterjalid=== |
===Kasulikud lisamaterjalid=== |
||
Redaktsioon: 26. mai 2026, kell 12:29
Sissejuhatus
Osutub, et tcp/ip network stack Linux, Windows, BSD jt implementatsioonid võimaldavad teatud tingimustel mitmel protsessil korraga sama pordi kasutada. Seda võimalust kehastab 'so_reuseport' socket option.
Tööpõhimõte
TODO
Väited
- haproxy - kasutatakse vaikimisi
- nginx - vaikimisi ei kasutata, aga soovitatakse kasutada jõudluse suurendamise eesmärgil
- zabbix-agent2 - ei ole võimalik kasutada
Kasutamine
Python skript
# cat socket-01-server.py
import socket
import os
import sys
PORT = 8083
# Fetch the Process ID so we can easily tell which instance answers the client
PID = os.getpid()
# 1. Create a standard TCP IPv4 Socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 2. THE MAGIC OPTIONS: Enable Reuse Address and Reuse Port
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# 3. Bind and Listen (Standard blocking mode)
server_socket.bind(('0.0.0.0', PORT))
server_socket.listen(10)
print(f"[Worker PID: {PID}] Multi-core listening gate opened on port {PORT}...")
try:
while True:
# This call BLOCKS completely. The script sleeps right here in the kernel
# until the Linux 4-tuple hash decides to route a client to THIS specific socket instance.
client_socket, client_address = server_socket.accept()
# Read the raw incoming HTTP request payload (up to 4KB)
request = client_socket.recv(4096)
if request:
# Construct a basic HTTP response containing the answering process ID
body = f"Hello from Worker Process ID: {PID}\n"
response = (
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
f"Content-Length: {len(body)}\r\n"
"Connection: close\r\n"
"\r\n"
f"{body}"
).encode('utf-8')
# Fire the response back to the client
client_socket.sendall(response)
# Close the socket immediately to return the client line
client_socket.close()
except KeyboardInterrupt:
print(f"\n[Worker {PID}] Shutting down down gracefully.")
finally:
server_socket.close()
Nginx
TODO
haproxy
TODO
Kasulikud lisamaterjalid
- TODO