Skip to content

Commit

Permalink
add ListerAtCloser
Browse files Browse the repository at this point in the history
The ListerAt is stored in the Request state and reused across requests.
Some implementations don't store the entire []os.FileInfo buffer in the
ListerAt implementation but instead return an open file and get/return
[]os.FileInfo on request. For these implementation a Close is required
  • Loading branch information
drakkan committed Feb 9, 2024
1 parent bae544b commit f5cb480
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions request-interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ type ListerAt interface {
ListAt([]os.FileInfo, int64) (int, error)
}

// ListerAtCloser is a ListerAt that implements the Close method.
type ListerAtCloser interface {
ListerAt
Close() error
}

// TransferError is an optional interface that readerAt and writerAt
// can implement to be notified about the error causing Serve() to exit
// with the request still open
Expand Down
30 changes: 30 additions & 0 deletions request-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,36 @@ func TestRequestReaddir(t *testing.T) {
checkRequestServerAllocator(t, p)
}

type testListerAtCloser struct {
isClosed bool
}

func (l *testListerAtCloser) ListAt([]os.FileInfo, int64) (int, error) {
return 0, io.EOF
}

func (l *testListerAtCloser) Close() error {
l.isClosed = true
return nil
}

func TestRequestServerListerAtCloser(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()

handle, err := p.cli.opendir(context.Background(), "/")
require.NoError(t, err)
require.Len(t, p.svr.openRequests, 1)
req, ok := p.svr.getRequest(handle)
require.True(t, ok)
listerAt := &testListerAtCloser{}
req.setListerAt(listerAt)
assert.NotNil(t, req.state.getListerAt())
p.cli.close(handle)
require.Len(t, p.svr.openRequests, 0)
assert.True(t, listerAt.isClosed)
}

func TestRequestStatVFS(t *testing.T) {
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
t.Skip("StatVFS is implemented on linux and darwin")
Expand Down
14 changes: 14 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ func (s *state) getListerAt() ListerAt {
return s.listerAt
}

func (s *state) closeListerAt() {
s.mu.Lock()
defer s.mu.Unlock()

if s.listerAt != nil {
if closer, ok := s.listerAt.(ListerAtCloser); ok {
closer.Close()
}
s.listerAt = nil
}
}

// Request contains the data and state for the incoming service request.
type Request struct {
// Get, Put, Setstat, Stat, Rename, Remove
Expand Down Expand Up @@ -229,6 +241,8 @@ func (r *Request) close() error {
}
}()

r.state.closeListerAt()

rd, wr, rw := r.getAllReaderWriters()

var err error
Expand Down

0 comments on commit f5cb480

Please sign in to comment.