-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: better apply tcp keepalive to listeners
- Loading branch information
Showing
30 changed files
with
180 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package keepalive | ||
|
||
import ( | ||
"net" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/metacubex/mihomo/common/atomic" | ||
"github.com/metacubex/mihomo/common/utils" | ||
) | ||
|
||
var ( | ||
keepAliveIdle = atomic.NewTypedValue[time.Duration](0 * time.Second) | ||
keepAliveInterval = atomic.NewTypedValue[time.Duration](0 * time.Second) | ||
disableKeepAlive = atomic.NewBool(false) | ||
|
||
SetDisableKeepAliveCallback = utils.NewCallback[bool]() | ||
) | ||
|
||
func SetKeepAliveIdle(t time.Duration) { | ||
keepAliveIdle.Store(t) | ||
} | ||
|
||
func SetKeepAliveInterval(t time.Duration) { | ||
keepAliveInterval.Store(t) | ||
} | ||
|
||
func KeepAliveIdle() time.Duration { | ||
return keepAliveIdle.Load() | ||
} | ||
|
||
func KeepAliveInterval() time.Duration { | ||
return keepAliveInterval.Load() | ||
} | ||
|
||
func SetDisableKeepAlive(disable bool) { | ||
if runtime.GOOS == "android" { | ||
setDisableKeepAlive(false) | ||
} else { | ||
setDisableKeepAlive(disable) | ||
} | ||
} | ||
|
||
func setDisableKeepAlive(disable bool) { | ||
disableKeepAlive.Store(disable) | ||
SetDisableKeepAliveCallback.Emit(disable) | ||
} | ||
|
||
func DisableKeepAlive() bool { | ||
return disableKeepAlive.Load() | ||
} | ||
|
||
func SetNetDialer(dialer *net.Dialer) { | ||
setNetDialer(dialer) | ||
} | ||
|
||
func SetNetListenConfig(lc *net.ListenConfig) { | ||
setNetListenConfig(lc) | ||
} | ||
|
||
func TCPKeepAlive(c net.Conn) { | ||
if tcp, ok := c.(*net.TCPConn); ok && tcp != nil { | ||
tcpKeepAlive(tcp) | ||
} | ||
} |
Oops, something went wrong.