Skip to content

Commit

Permalink
Thread context.Context into Serve()
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaelatern committed Dec 13, 2024
1 parent 62a210f commit 114ef4c
Showing 1 changed file with 45 additions and 18 deletions.
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

0 comments on commit 114ef4c

Please sign in to comment.