New function IsNetworkErrorFatal to invalidate connections on fatal network errors. Use connection.Expires field to trigger removal of inactive connections after a while.

This commit is contained in:
Kleissner
2021-03-01 01:05:37 +01:00
parent 9086e201c3
commit dcc7464f46
3 changed files with 34 additions and 7 deletions

View File

@@ -6,7 +6,10 @@ Author: Peter Kleissner
package core
import "net"
import (
"net"
"strings"
)
// FindInterfaceByIP finds an interface based on the IP. The IP must be available at the interface.
func FindInterfaceByIP(ip net.IP) (iface *net.Interface, ipnet *net.IPNet) {
@@ -69,3 +72,18 @@ func IsIPv4(IP net.IP) bool {
func IsIPv6(IP net.IP) bool {
return IP.To4() == nil && IP.To16() != nil
}
// IsNetworkErrorFatal checks if a network error indicates a broken connection.
// Not every network error indicates a broken connection. This function prevents from over-dropping connections.
func IsNetworkErrorFatal(err error) bool {
if err == nil {
return false
}
// Windows: A common error when the network adapter is disabled is "wsasendto: The requested address is not valid in its context".
if strings.Contains(err.Error(), "requested address is not valid in its context") {
return true
}
return false
}