mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-04 00:25:04 +00:00
configuration for connection reuse
This commit is contained in:
1 parent
6ce7b1d532
commit
a86cd36ad2
9 files changed
+124
-7
No files matched your search
@@ -0,0 +1,16 @@
|
||||
package transport
|
||||
|
||||
type StreamType int
|
||||
|
||||
const (
|
||||
StreamTypeTCP = StreamType(0)
|
||||
)
|
||||
|
||||
type TCPConfig struct {
|
||||
ConnectionReuse bool
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
StreamType StreamType
|
||||
TCPConfig *TCPConfig
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// +build json
|
||||
|
||||
package transport
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (this *Config) UnmarshalJSON(data []byte) error {
|
||||
type TypeConfig struct {
|
||||
StreamType string `json:"streamType"`
|
||||
Settings json.RawMessage `json:"settings"`
|
||||
}
|
||||
type JsonTCPConfig struct {
|
||||
ConnectionReuse bool `json:"connectionReuse"`
|
||||
}
|
||||
|
||||
typeConfig := new(TypeConfig)
|
||||
if err := json.Unmarshal(data, typeConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
streamType := strings.ToLower(typeConfig.StreamType)
|
||||
if streamType == "tcp" {
|
||||
jsonTCPConfig := new(JsonTCPConfig)
|
||||
if err := json.Unmarshal(data, jsonTCPConfig); err != nil {
|
||||
return err
|
||||
}
|
||||
this.TCPConfig = &TCPConfig{
|
||||
ConnectionReuse: jsonTCPConfig.ConnectionReuse,
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package hub
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/v2ray/v2ray-core/transport"
|
||||
)
|
||||
|
||||
type ConnectionHandler func(*Connection)
|
||||
@@ -37,7 +39,7 @@ func (this *Connection) Close() error {
|
||||
if this == nil || this.conn == nil {
|
||||
return ErrorClosedConnection
|
||||
}
|
||||
if this.Reusable() {
|
||||
if transport.TCPStreamConfig.ConnectionReuse && this.Reusable() {
|
||||
this.listener.Recycle(this.dest, this.conn)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,9 +3,33 @@ package hub
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Once struct {
|
||||
m sync.Mutex
|
||||
done uint32
|
||||
}
|
||||
|
||||
func (o *Once) Do(f func()) {
|
||||
if atomic.LoadUint32(&o.done) == 1 {
|
||||
return
|
||||
}
|
||||
o.m.Lock()
|
||||
defer o.m.Unlock()
|
||||
if o.done == 0 {
|
||||
defer atomic.StoreUint32(&o.done, 1)
|
||||
f()
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Once) Reset() {
|
||||
o.m.Lock()
|
||||
defer o.m.Unlock()
|
||||
atomic.StoreUint32(&o.done, 0)
|
||||
}
|
||||
|
||||
type AwaitingConnection struct {
|
||||
conn net.Conn
|
||||
expire time.Time
|
||||
@@ -17,19 +41,20 @@ func (this *AwaitingConnection) Expired() bool {
|
||||
|
||||
type ConnectionCache struct {
|
||||
sync.Mutex
|
||||
cache map[string][]*AwaitingConnection
|
||||
cache map[string][]*AwaitingConnection
|
||||
cleanupOnce Once
|
||||
}
|
||||
|
||||
func NewConnectionCache() *ConnectionCache {
|
||||
c := &ConnectionCache{
|
||||
return &ConnectionCache{
|
||||
cache: make(map[string][]*AwaitingConnection),
|
||||
}
|
||||
go c.Cleanup()
|
||||
return c
|
||||
}
|
||||
|
||||
func (this *ConnectionCache) Cleanup() {
|
||||
for {
|
||||
defer this.cleanupOnce.Reset()
|
||||
|
||||
for len(this.cache) > 0 {
|
||||
time.Sleep(time.Second * 4)
|
||||
this.Lock()
|
||||
for key, value := range this.cache {
|
||||
@@ -74,6 +99,8 @@ func (this *ConnectionCache) Recycle(dest string, conn net.Conn) {
|
||||
list = []*AwaitingConnection{aconn}
|
||||
}
|
||||
this.cache[dest] = list
|
||||
|
||||
go this.cleanupOnce.Do(this.Cleanup)
|
||||
}
|
||||
|
||||
func FindFirstValid(list []*AwaitingConnection) int {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/transport"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -17,7 +18,10 @@ var (
|
||||
|
||||
func Dial(dest v2net.Destination) (*Connection, error) {
|
||||
destStr := dest.String()
|
||||
conn := globalCache.Get(destStr)
|
||||
var conn net.Conn
|
||||
if transport.TCPStreamConfig.ConnectionReuse {
|
||||
conn = globalCache.Get(destStr)
|
||||
}
|
||||
if conn == nil {
|
||||
var err error
|
||||
log.Debug("Hub: Dialling new connection to ", dest)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package transport
|
||||
|
||||
import "github.com/v2ray/v2ray-core/common/log"
|
||||
|
||||
var (
|
||||
TCPStreamConfig = &TCPConfig{
|
||||
ConnectionReuse: false,
|
||||
}
|
||||
)
|
||||
|
||||
func ApplyConfig(config *Config) error {
|
||||
if config.StreamType == StreamTypeTCP {
|
||||
if config.TCPConfig != nil {
|
||||
TCPStreamConfig = config.TCPConfig
|
||||
if config.TCPConfig.ConnectionReuse {
|
||||
log.Info("Transport: TCP connection reuse enabled.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user