Da Libuv v1.35.0 (Knoten
v13.12.0&v12.17.0) sowohlTCP_KEEPINTVLals auchTCP_KEEPCNTetwas vorhersehbare Werte haben, ermöglicht Ihnen dieses Paket, diese Werte jeweils pro Steckdose zu optimieren, aber mit Kosten, um mit FFI -Overhead und IT -Abhängigkeiten zu handeln. Überprüfen Sie die neuesten Knoten -Dokumente für Socket.SetkeePaliveable. Wenn die Standardwerte für Sie gut genug sind, müssen Sie dieses Paket nicht verwenden.
Die fehlenden ( TCP_KEEPINTVL und TCP_KEEPCNT ) SO_KEEPALIVE Socket -Options -Setter und Getters für Knoten mit FFI.
Getestet auf linux & osx (sowohl amd64 als auch arm64 ) sollten auf freebsd und anderen arbeiten. Installationen auf win32 , aber Methoden sind No-Ops (Zugangebote willkommen).
Es wird auch unterstützt, dass die Option TCP_USER_Timeout (nur Linux und osx ) das TCP_USER_TIMEOUT ( linux und OSX) eingestellt wird, das eng mit Keep-Alive zusammenhängt.
| Plattform | Tcp_keepintvl | Tcp_keepcnt | Tcp_user_timeout |
|---|---|---|---|
linux | |||
osx | ( TCP_RXT_CONNDROPTIME ) | ||
freebsd | |||
win32 |
Legende:
npm install --save net-keepaliveDas vollständige API -Referenzdokument (JSDOC) finden Sie auf unseren Github -Seiten.
Das Projekt enthält die Datei für Typscript -Definitionen ( index.d.ts ), die einen Überblick über die exponierte API gibt.
Die Dokumentation wird aus JSDOC -Kommentaren generiert. Sie können sie gerne verbessern, indem Sie Pull -Anfragen senden.
const Net = require ( 'net' ) ,
NetKeepAlive = require ( 'net-keepalive' )
// or
import * as Net from 'net'
import * as NetKeepAlive from 'net-keepalive'
// Create a TCP Server
const srv = Net . createServer ( ( s ) => {
console . log ( 'Connected %j' , s . address ( ) )
// Doesn't matter what it does
s . pipe ( s )
} )
// Start on some port
srv . listen ( 1337 , ( ) => {
console . log ( 'Listening on %j' , srv . address ( ) )
} )
// Connect to that server
const s = Net . createConnection ( { port : 1337 } , ( ) => {
console . log ( 'Connected to %j' , s . address ( ) )
//IMPORTANT: KeepAlive must be enabled for this to work
s . setKeepAlive ( true , 1000 )
// Set TCP_KEEPINTVL for this specific socket
NetKeepAlive . setKeepAliveInterval ( s , 1000 )
// Get TCP_KEEPINTVL for this specific socket
NetKeepAlive . getKeepAliveInterval ( s ) // 1000
// Set TCP_KEEPCNT for this specific socket
NetKeepAlive . setKeepAliveProbes ( s , 1 )
// Get TCP_KEEPCNT for this specific socket
NetKeepAlive . getKeepAliveProbes ( s ) // 1
} ) Verwenden Sie nun iptables Regel, um alle tcp -Pakete in INPUT auf Port 1337 zu fallen.
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP Wenn Sie Pakete auf loopback mit tcp.srcport == 1337 || tcp.dstport == 1337 überwacht haben tcp.srcport == 1337 || tcp.dstport == 1337 Filter in wireshark . Sie sehen die folgende Ausgabe:
Viel Spaß!
Weitere Informationen zu SO_KEEPALIVE hier: TCP Keepalive Howto C -Code -Beispiele hier: Beispiele
Hinweis: Damit diese Methoden funktionieren socket.setKeepAlive([enable][, initialDelay]) müssen Sie SO_KEEPALIVE aktivieren und die Optionen TCP_KEEPIDLE für Socket mithilfe von Net.Socket -S einstellen.
TCP_KEEPIDLE (since Linux 2.4) The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.
const NetKeepAlive = require ( 'net-keepalive' )
// or
import * as NetKeepAlive from 'net-keepalive'
// .....
const enable = true // enable SO_KEEPALIVE
const initialDuration = 1000 // start probing after 1 second of inactivity
socket . setKeepAlive ( enable , initialDuration ) // sets SO_KEEPALIVE and TCP_KEEPIDLE
const probeInterval = 1000 // after initialDuration send probes every 1 second
NetKeepAlive . setKeepAliveInterval ( socket , probeInterval ) //sets TCP_KEEPINTVL
const maxProbesBeforeFail = 10 // after 10 failed probes connection will be dropped
NetKeepAlive . setKeepAliveProbes ( socket , maxProbesBeforeFail ) // sets TCP_KEEPCNT
// .... Siehe code_of_conduct.md
Siehe Beitrag.md
Vielen Dank an diese wunderbaren Menschen (Emoji -Schlüssel):
George Hertz | Alba Mendez | Paulo Castro | Jacob Jewell | Rmutharaju | Rafael Borges | Calvin |
GGSubs | Mario Kozjak | Lukas Knuth | Ivan | Otávio Jacobi |
Dieses Projekt folgt der All-Contributors-Spezifikation. Beiträge jeglicher Art willkommen!