1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-17 23:06:30 -05:00

fix lint warnings

This commit is contained in:
Darien Raymond 2017-01-01 22:12:44 +01:00
parent 4d6ca7efe0
commit 48bddb25d7
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
10 changed files with 63 additions and 51 deletions

View File

@ -9,50 +9,59 @@ import (
"v2ray.com/core/common/signal" "v2ray.com/core/common/signal"
) )
// ConnectionRecyler is the interface for recycling connections.
type ConnectionRecyler interface { type ConnectionRecyler interface {
Put(ConnectionId, net.Conn) // Put returns a connection back to a connection pool.
Put(ConnectionID, net.Conn)
} }
type ConnectionId struct { // ConnectionID is the ID of a connection.
type ConnectionID struct {
Local v2net.Address Local v2net.Address
Remote v2net.Address Remote v2net.Address
RemotePort v2net.Port RemotePort v2net.Port
} }
func NewConnectionId(source v2net.Address, dest v2net.Destination) ConnectionId { // NewConnectionID creates a new ConnectionId.
return ConnectionId{ func NewConnectionID(source v2net.Address, dest v2net.Destination) ConnectionID {
return ConnectionID{
Local: source, Local: source,
Remote: dest.Address, Remote: dest.Address,
RemotePort: dest.Port, RemotePort: dest.Port,
} }
} }
// ExpiringConnection is a connection that will expire in certain time.
type ExpiringConnection struct { type ExpiringConnection struct {
conn net.Conn conn net.Conn
expire time.Time expire time.Time
} }
func (o *ExpiringConnection) Expired() bool { // Expired returns true if the connection has expired.
return o.expire.Before(time.Now()) func (ec *ExpiringConnection) Expired() bool {
return ec.expire.Before(time.Now())
} }
// Pool is a connection pool.
type Pool struct { type Pool struct {
sync.Mutex sync.Mutex
connsByDest map[ConnectionId][]*ExpiringConnection connsByDest map[ConnectionID][]*ExpiringConnection
cleanupOnce signal.Once cleanupOnce signal.Once
} }
// NewConnectionPool creates a new Pool.
func NewConnectionPool() *Pool { func NewConnectionPool() *Pool {
return &Pool{ return &Pool{
connsByDest: make(map[ConnectionId][]*ExpiringConnection), connsByDest: make(map[ConnectionID][]*ExpiringConnection),
} }
} }
func (o *Pool) Get(id ConnectionId) net.Conn { // Get returns a connection with matching connection ID. Nil if not found.
o.Lock() func (p *Pool) Get(id ConnectionID) net.Conn {
defer o.Unlock() p.Lock()
defer p.Unlock()
list, found := o.connsByDest[id] list, found := p.connsByDest[id]
if !found { if !found {
return nil return nil
} }
@ -72,18 +81,18 @@ func (o *Pool) Get(id ConnectionId) net.Conn {
list[connIdx] = list[listLen-1] list[connIdx] = list[listLen-1]
} }
list = list[:listLen-1] list = list[:listLen-1]
o.connsByDest[id] = list p.connsByDest[id] = list
return conn.conn return conn.conn
} }
func (o *Pool) Cleanup() { func (p *Pool) cleanup() {
defer o.cleanupOnce.Reset() defer p.cleanupOnce.Reset()
for len(o.connsByDest) > 0 { for len(p.connsByDest) > 0 {
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
expiredConns := make([]net.Conn, 0, 16) expiredConns := make([]net.Conn, 0, 16)
o.Lock() p.Lock()
for dest, list := range o.connsByDest { for dest, list := range p.connsByDest {
validConns := make([]*ExpiringConnection, 0, len(list)) validConns := make([]*ExpiringConnection, 0, len(list))
for _, conn := range list { for _, conn := range list {
if conn.Expired() { if conn.Expired() {
@ -93,34 +102,35 @@ func (o *Pool) Cleanup() {
} }
} }
if len(validConns) != len(list) { if len(validConns) != len(list) {
o.connsByDest[dest] = validConns p.connsByDest[dest] = validConns
} }
} }
o.Unlock() p.Unlock()
for _, conn := range expiredConns { for _, conn := range expiredConns {
conn.Close() conn.Close()
} }
} }
} }
func (o *Pool) Put(id ConnectionId, conn net.Conn) { // Put implements ConnectionRecyler.Put().
func (p *Pool) Put(id ConnectionID, conn net.Conn) {
expiringConn := &ExpiringConnection{ expiringConn := &ExpiringConnection{
conn: conn, conn: conn,
expire: time.Now().Add(time.Second * 4), expire: time.Now().Add(time.Second * 4),
} }
o.Lock() p.Lock()
defer o.Unlock() defer p.Unlock()
list, found := o.connsByDest[id] list, found := p.connsByDest[id]
if !found { if !found {
list = []*ExpiringConnection{expiringConn} list = []*ExpiringConnection{expiringConn}
} else { } else {
list = append(list, expiringConn) list = append(list, expiringConn)
} }
o.connsByDest[id] = list p.connsByDest[id] = list
o.cleanupOnce.Do(func() { p.cleanupOnce.Do(func() {
go o.Cleanup() go p.cleanup()
}) })
} }

View File

@ -51,11 +51,11 @@ func TestConnectionCache(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)
pool := NewConnectionPool() pool := NewConnectionPool()
conn := pool.Get(NewConnectionId(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80)))) conn := pool.Get(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))))
assert.Pointer(conn).IsNil() assert.Pointer(conn).IsNil()
pool.Put(NewConnectionId(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))), &TestConnection{id: "test"}) pool.Put(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))), &TestConnection{id: "test"})
conn = pool.Get(NewConnectionId(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80)))) conn = pool.Get(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))))
assert.String(conn.(*TestConnection).id).Equals("test") assert.String(conn.(*TestConnection).id).Equals("test")
} }
@ -64,9 +64,9 @@ func TestConnectionRecycle(t *testing.T) {
pool := NewConnectionPool() pool := NewConnectionPool()
c := &TestConnection{id: "test"} c := &TestConnection{id: "test"}
pool.Put(NewConnectionId(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))), c) pool.Put(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))), c)
time.Sleep(6 * time.Second) time.Sleep(6 * time.Second)
assert.Bool(c.closed).IsTrue() assert.Bool(c.closed).IsTrue()
conn := pool.Get(NewConnectionId(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80)))) conn := pool.Get(NewConnectionID(v2net.LocalHostIP, v2net.TCPDestination(v2net.LocalHostIP, v2net.Port(80))))
assert.Pointer(conn).IsNil() assert.Pointer(conn).IsNil()
} }

View File

@ -3,13 +3,15 @@ package internal
import ( import (
"net" "net"
"reflect" "reflect"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
) )
var ( var (
ErrInvalidConn = errors.New("Invalid Connection.") errInvalidConn = errors.New("Invalid Connection.")
) )
// GetSysFd returns the underlying fd of a connection.
func GetSysFd(conn net.Conn) (int, error) { func GetSysFd(conn net.Conn) (int, error) {
cv := reflect.ValueOf(conn) cv := reflect.ValueOf(conn)
switch ce := cv.Elem(); ce.Kind() { switch ce := cv.Elem(); ce.Kind() {
@ -21,5 +23,5 @@ func GetSysFd(conn net.Conn) (int, error) {
return int(fd.Int()), nil return int(fd.Int()), nil
} }
} }
return 0, ErrInvalidConn return 0, errInvalidConn
} }

View File

@ -156,7 +156,7 @@ func (v *Updater) Run() {
type SystemConnection interface { type SystemConnection interface {
net.Conn net.Conn
Id() internal.ConnectionId Id() internal.ConnectionID
Reset(func([]Segment)) Reset(func([]Segment))
Overhead() int Overhead() int
} }

View File

@ -48,15 +48,15 @@ func (o *NoOpConn) SetWriteDeadline(time.Time) error {
return nil return nil
} }
func (o *NoOpConn) Id() internal.ConnectionId { func (o *NoOpConn) Id() internal.ConnectionID {
return internal.ConnectionId{} return internal.ConnectionID{}
} }
func (o *NoOpConn) Reset(input func([]Segment)) {} func (o *NoOpConn) Reset(input func([]Segment)) {}
type NoOpRecycler struct{} type NoOpRecycler struct{}
func (o *NoOpRecycler) Put(internal.ConnectionId, net.Conn) {} func (o *NoOpRecycler) Put(internal.ConnectionID, net.Conn) {}
func TestConnectionReadTimeout(t *testing.T) { func TestConnectionReadTimeout(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)

View File

@ -26,7 +26,7 @@ var (
type ClientConnection struct { type ClientConnection struct {
sync.RWMutex sync.RWMutex
net.Conn net.Conn
id internal.ConnectionId id internal.ConnectionID
input func([]Segment) input func([]Segment)
reader PacketReader reader PacketReader
writer PacketWriter writer PacketWriter
@ -56,7 +56,7 @@ func (o *ClientConnection) Read([]byte) (int, error) {
panic("KCP|ClientConnection: Read should not be called.") panic("KCP|ClientConnection: Read should not be called.")
} }
func (o *ClientConnection) Id() internal.ConnectionId { func (o *ClientConnection) Id() internal.ConnectionID {
return o.id return o.id
} }
@ -112,7 +112,7 @@ func DialKCP(src v2net.Address, dest v2net.Destination, options internet.DialerO
dest.Network = v2net.Network_UDP dest.Network = v2net.Network_UDP
log.Info("KCP|Dialer: Dialing KCP to ", dest) log.Info("KCP|Dialer: Dialing KCP to ", dest)
id := internal.NewConnectionId(src, dest) id := internal.NewConnectionID(src, dest)
conn := globalPool.Get(id) conn := globalPool.Get(id)
if conn == nil { if conn == nil {
rawConn, err := internet.DialToDest(src, dest) rawConn, err := internet.DialToDest(src, dest)

View File

@ -27,7 +27,7 @@ type ConnectionID struct {
} }
type ServerConnection struct { type ServerConnection struct {
id internal.ConnectionId id internal.ConnectionID
local net.Addr local net.Addr
remote net.Addr remote net.Addr
writer PacketWriter writer PacketWriter
@ -73,7 +73,7 @@ func (o *ServerConnection) SetWriteDeadline(time.Time) error {
return nil return nil
} }
func (o *ServerConnection) Id() internal.ConnectionId { func (o *ServerConnection) Id() internal.ConnectionID {
return o.id return o.id
} }
@ -188,7 +188,7 @@ func (v *Listener) OnReceive(payload *buf.Buffer, session *proxy.SessionInfo) {
} }
localAddr := v.hub.Addr() localAddr := v.hub.Addr()
sConn := &ServerConnection{ sConn := &ServerConnection{
id: internal.NewConnectionId(v2net.LocalHostIP, src), id: internal.NewConnectionID(v2net.LocalHostIP, src),
local: localAddr, local: localAddr,
remote: remoteAddr, remote: remoteAddr,
writer: &KCPPacketWriter{ writer: &KCPPacketWriter{
@ -274,7 +274,7 @@ func (v *Listener) Addr() net.Addr {
return v.hub.Addr() return v.hub.Addr()
} }
func (v *Listener) Put(internal.ConnectionId, net.Conn) {} func (v *Listener) Put(internal.ConnectionID, net.Conn) {}
type Writer struct { type Writer struct {
id ConnectionID id ConnectionID

View File

@ -9,7 +9,7 @@ import (
) )
type ConnectionManager interface { type ConnectionManager interface {
Put(internal.ConnectionId, net.Conn) Put(internal.ConnectionID, net.Conn)
} }
type RawConnection struct { type RawConnection struct {
@ -27,14 +27,14 @@ func (v *RawConnection) SysFd() (int, error) {
} }
type Connection struct { type Connection struct {
id internal.ConnectionId id internal.ConnectionID
reusable bool reusable bool
conn net.Conn conn net.Conn
listener ConnectionManager listener ConnectionManager
config *Config config *Config
} }
func NewConnection(id internal.ConnectionId, conn net.Conn, manager ConnectionManager, config *Config) *Connection { func NewConnection(id internal.ConnectionID, conn net.Conn, manager ConnectionManager, config *Config) *Connection {
return &Connection{ return &Connection{
id: id, id: id,
conn: conn, conn: conn,

View File

@ -27,7 +27,7 @@ func Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOpti
} }
tcpSettings := networkSettings.(*Config) tcpSettings := networkSettings.(*Config)
id := internal.NewConnectionId(src, dest) id := internal.NewConnectionID(src, dest)
var conn net.Conn var conn net.Conn
if dest.Network == v2net.Network_TCP && tcpSettings.IsConnectionReuse() { if dest.Network == v2net.Network_TCP && tcpSettings.IsConnectionReuse() {
conn = globalCache.Get(id) conn = globalCache.Get(id)

View File

@ -89,7 +89,7 @@ func (v *TCPListener) Accept() (internet.Connection, error) {
return nil, connErr.err return nil, connErr.err
} }
conn := connErr.conn conn := connErr.conn
return NewConnection(internal.ConnectionId{}, conn, v, v.config), nil return NewConnection(internal.ConnectionID{}, conn, v, v.config), nil
case <-time.After(time.Second * 2): case <-time.After(time.Second * 2):
} }
} }
@ -125,7 +125,7 @@ func (v *TCPListener) KeepAccepting() {
} }
} }
func (v *TCPListener) Put(id internal.ConnectionId, conn net.Conn) { func (v *TCPListener) Put(id internal.ConnectionID, conn net.Conn) {
v.Lock() v.Lock()
defer v.Unlock() defer v.Unlock()
if !v.acccepting { if !v.acccepting {