Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread context.Context into Serve() #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 45 additions & 18 deletions fs/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,11 @@ type Server struct {
wg sync.WaitGroup
}

// Serve serves the FUSE connection by making calls to the methods
// of fs and the Nodes and Handles it makes available. It returns only
// when the connection has been closed or an unexpected error occurs.
func (s *Server) Serve(fs FS) error {
// ServeContext serves the FUSE connection by making calls to the methods
// of fs and the Nodes and Handles it makes available. It returns when the
// context is closed, when the connection has been closed, or an unexpected
// error occurs.
func (s *Server) ServeContext(ctx context.Context, fs FS) error {
defer s.wg.Wait() // Wait for worker goroutines to complete before return

s.fs = fs
Expand All @@ -507,22 +508,48 @@ func (s *Server) Serve(fs FS) error {
})
s.handle = append(s.handle, nil)

for {
req, err := s.conn.ReadRequest()
if err != nil {
if err == io.EOF {
break
retErr := make(chan error)

go func() {
for {
req, err := s.conn.ReadRequest()
if err != nil {
if err == io.EOF {
break
}
retErr <- err
return
}
return err

s.wg.Add(1)
go func() {
defer s.wg.Done()
s.serve(ctx, req)
}()
}
retErr <- nil
}()

s.wg.Add(1)
go func() {
defer s.wg.Done()
s.serve(req)
}()
select {
case err := <-retErr:
return err
case <-ctx.Done():
return nil
}
return nil
}

// Serve serves the FUSE connection by making calls to the methods
// of fs and the Nodes and Handles it makes available. It returns only
// when the connection has been closed or an unexpected error occurs.
func (s *Server) Serve(fs FS) error {
return s.ServeContext(context.Background(), fs)
}

// ServeContext serves a FUSE connection with the default settings. See
// Server.ServeContext.
func ServeContext(ctx context.Context, c *fuse.Conn, fs FS) error {
server := New(c, nil)
return server.ServeContext(ctx, fs)
}

// Serve serves a FUSE connection with the default settings. See
Expand Down Expand Up @@ -915,8 +942,8 @@ func (m *logDuplicateRequestID) String() string {
return fmt.Sprintf("Duplicate request: new %v, old %v", m.New, m.Old)
}

func (c *Server) serve(r fuse.Request) {
ctx, cancel := context.WithCancel(context.Background())
func (c *Server) serve(originContext context.Context, r fuse.Request) {
ctx, cancel := context.WithCancel(originContext)
defer cancel()
parentCtx := ctx
if c.context != nil {
Expand Down