Optimization: use client session cache for TLS connection (#26)

This commit is contained in:
changx
2018-11-01 11:54:45 +08:00
committed by Dreamacro
parent c5757a9b11
commit fd63707399
4 changed files with 67 additions and 19 deletions

View File

@@ -1,10 +1,12 @@
package adapters
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"sync"
"time"
C "github.com/Dreamacro/clash/constant"
@@ -14,6 +16,11 @@ const (
tcpTimeout = 5 * time.Second
)
var (
globalClientSessionCache tls.ClientSessionCache
once sync.Once
)
// DelayTest get the delay for the specified URL
func DelayTest(proxy C.Proxy, url string) (t int16, err error) {
addr, err := urlToMetadata(url)
@@ -95,3 +102,10 @@ func tcpKeepAlive(c net.Conn) {
tcp.SetKeepAlivePeriod(30 * time.Second)
}
}
func getClientSessionCache() tls.ClientSessionCache {
once.Do(func() {
globalClientSessionCache = tls.NewLRUClientSessionCache(128)
})
return globalClientSessionCache
}