Skip to content

Commit

Permalink
Merge pull request #6 from datawire/thallgren/optimize
Browse files Browse the repository at this point in the history
Optimizations and removal of dlib
  • Loading branch information
thallgren authored May 3, 2023
2 parents e265865 + ab239e6 commit dadf858
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 253 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/datawire/go-fuseftp
go 1.20

require (
github.com/datawire/dlib v1.3.1-0.20220715022530-b09ab2e017e1
github.com/datawire/go-ftpserver v0.1.3
github.com/datawire/go-fuseftp/rpc v0.3.1
github.com/jlaffaye/ftp v0.1.0
Expand All @@ -16,11 +15,12 @@ require (
)

require (
github.com/datawire/dlib v1.3.1-0.20220715022530-b09ab2e017e1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fclairamb/ftpserverlib v0.21.0 // indirect
github.com/fclairamb/go-log v0.4.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down
40 changes: 18 additions & 22 deletions pkg/fs/connpool.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package fs

import (
"context"
"net"
"net/netip"
"strings"
"sync"
"time"

"github.com/jlaffaye/ftp"

"github.com/datawire/dlib/dlog"
log "github.com/sirupsen/logrus"
)

type connList struct {
Expand Down Expand Up @@ -69,10 +67,8 @@ type connPool struct {
}

// connect returns a new connection without using the pool. Use get instead of connect.
func (p *connPool) connect(ctx context.Context) (*ftp.ServerConn, error) {
opts := []ftp.DialOption{
ftp.DialWithContext(ctx),
}
func (p *connPool) connect() (*ftp.ServerConn, error) {
var opts []ftp.DialOption
if p.timeout > 0 {
opts = append(opts,
ftp.DialWithTimeout(p.timeout),
Expand Down Expand Up @@ -109,7 +105,7 @@ func (p *connPool) connect(ctx context.Context) (*ftp.ServerConn, error) {
}

// get returns a connection from the pool, or creates a new connection if needed
func (p *connPool) get(ctx context.Context) (conn *ftp.ServerConn, err error) {
func (p *connPool) get() (conn *ftp.ServerConn, err error) {
p.Lock()
if idle := p.idleList; idle != nil {
p.idleList = idle.next
Expand All @@ -119,14 +115,14 @@ func (p *connPool) get(ctx context.Context) (conn *ftp.ServerConn, err error) {
}
p.Unlock()
if conn == nil {
conn, err = p.connect(ctx)
conn, err = p.connect()
}
return
}

// setAddr will call Quit on all open connections, both busy and idle, change
// the address, reconnect one connection, and put it in the idle list.
func (p *connPool) setAddr(ctx context.Context, addr netip.AddrPort) error {
func (p *connPool) setAddr(addr netip.AddrPort) error {
var idle []*ftp.ServerConn
var busy []*ftp.ServerConn
p.Lock()
Expand All @@ -142,21 +138,21 @@ func (p *connPool) setAddr(ctx context.Context, addr netip.AddrPort) error {
if eq {
return nil
}
closeList(ctx, idle, true)
closeList(ctx, busy, true)
closeList(idle, true)
closeList(busy, true)

// Create the first connection up front, so that a failure to connect to the server is caught early
conn, err := p.connect(ctx)
conn, err := p.connect()
if err != nil {
return err
}
// return the connection to the pool
p.put(ctx, conn)
p.put(conn)
return nil
}

// put returns a connection to the pool
func (p *connPool) put(ctx context.Context, conn *ftp.ServerConn) {
func (p *connPool) put(conn *ftp.ServerConn) {
// remove from busyList
p.Lock()
removed := false
Expand Down Expand Up @@ -188,32 +184,32 @@ func (p *connPool) put(ctx context.Context, conn *ftp.ServerConn) {
p.Unlock()
}

func closeList(ctx context.Context, conns []*ftp.ServerConn, silent bool) {
func closeList(conns []*ftp.ServerConn, silent bool) {
for _, c := range conns {
if err := c.Quit(); err != nil && !silent {
if !strings.Contains(err.Error(), "use of closed") {
dlog.Errorf(ctx, "quit failed: %v", err)
log.Errorf("quit failed: %v", err)
}
}
}
}

// quit calls the Quit method on all connections and empties the pool
func (p *connPool) quit(ctx context.Context) {
func (p *connPool) quit() {
p.Lock()
idle := p.idleList.conns()
busy := p.idleList.conns()
p.idleList = nil
p.busyList = nil
p.Unlock()
closeList(ctx, idle, false)
closeList(ctx, busy, false)
closeList(idle, false)
closeList(busy, false)
}

// tidy will attempt to shrink the number of open connections to two, but since it
// only closes connections that are idle at the time the call is made, there
// might still be more than 2 connections after the call returns.
func (p *connPool) tidy(ctx context.Context) {
func (p *connPool) tidy() {
p.Lock()
idle := p.idleList.conns()
idleCount := 64 - p.busyList.size()
Expand All @@ -226,5 +222,5 @@ func (p *connPool) tidy(ctx context.Context) {
}
}
p.Unlock()
closeList(ctx, cl, false)
closeList(cl, false)
}
Loading

0 comments on commit dadf858

Please sign in to comment.