From 0985694a4b2fd1df66874f6ccfa5f41ac6458d7c Mon Sep 17 00:00:00 2001 From: Aaron Jacobs Date: Fri, 1 May 2015 11:17:15 +1000 Subject: [PATCH] Allow setting a parent context for all ops. --- connection.go | 14 +++++++++++--- fuseops/common_op.go | 7 ++++--- fuseops/convert.go | 5 ++++- mounted_file_system.go | 12 +++++++++++- 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/connection.go b/connection.go index 6083b22..cc0b657 100644 --- a/connection.go +++ b/connection.go @@ -21,6 +21,8 @@ import ( "runtime" "sync" + "golang.org/x/net/context" + "github.com/jacobsa/bazilfuse" "github.com/jacobsa/fuse/fuseops" ) @@ -31,6 +33,9 @@ type Connection struct { wrapped *bazilfuse.Conn opsInFlight sync.WaitGroup + // The context from which all op contexts inherit. + parentCtx context.Context + // For logging purposes only. nextOpID uint32 } @@ -38,11 +43,13 @@ type Connection struct { // Responsibility for closing the wrapped connection is transferred to the // result. You must call c.close() eventually. func newConnection( + parentCtx context.Context, logger *log.Logger, wrapped *bazilfuse.Conn) (c *Connection, err error) { c = &Connection{ - logger: logger, - wrapped: wrapped, + logger: logger, + wrapped: wrapped, + parentCtx: parentCtx, } return @@ -115,7 +122,8 @@ func (c *Connection) ReadOp() (op fuseops.Op, err error) { c.log(opID, calldepth+1, format, v...) } - if op = fuseops.Convert(bfReq, logForOp, &c.opsInFlight); op == nil { + op = fuseops.Convert(c.parentCtx, bfReq, logForOp, &c.opsInFlight) + if op == nil { c.log(opID, 1, "-> ENOSYS: %v", bfReq) bfReq.RespondError(ENOSYS) continue diff --git a/fuseops/common_op.go b/fuseops/common_op.go index 99364d1..204016b 100644 --- a/fuseops/common_op.go +++ b/fuseops/common_op.go @@ -25,12 +25,13 @@ import ( // A helper for embedding common behavior. type commonOp struct { - ctx context.Context opType string r bazilfuse.Request log func(int, string, ...interface{}) opsInFlight *sync.WaitGroup - report reqtrace.ReportFunc + + ctx context.Context + report reqtrace.ReportFunc } func describeOpType(t reflect.Type) (desc string) { @@ -40,13 +41,13 @@ func describeOpType(t reflect.Type) (desc string) { } func (o *commonOp) init( + ctx context.Context, opType reflect.Type, r bazilfuse.Request, log func(int, string, ...interface{}), opsInFlight *sync.WaitGroup) { // Initialize basic fields. o.opType = describeOpType(opType) - o.ctx = context.Background() o.r = r o.log = log o.opsInFlight = opsInFlight diff --git a/fuseops/convert.go b/fuseops/convert.go index 8dc7ac8..ef4a297 100644 --- a/fuseops/convert.go +++ b/fuseops/convert.go @@ -19,6 +19,8 @@ import ( "sync" "time" + "golang.org/x/net/context" + "github.com/jacobsa/bazilfuse" ) @@ -28,6 +30,7 @@ import ( // This function is an implementation detail of the fuse package, and must not // be called by anyone else. func Convert( + parentCtx context.Context, r bazilfuse.Request, logForOp func(int, string, ...interface{}), opsInFlight *sync.WaitGroup) (o Op) { @@ -213,7 +216,7 @@ func Convert( return } - co.init(reflect.TypeOf(o), r, logForOp, opsInFlight) + co.init(parentCtx, reflect.TypeOf(o), r, logForOp, opsInFlight) return } diff --git a/mounted_file_system.go b/mounted_file_system.go index 6772793..f814378 100644 --- a/mounted_file_system.go +++ b/mounted_file_system.go @@ -61,6 +61,10 @@ func (mfs *MountedFileSystem) Join(ctx context.Context) error { // Optional configuration accepted by Mount. type MountConfig struct { + // The context from which every op read from the connetion by the sever + // should inherit. If nil, context.Background() will be used. + OpContext context.Context + // OS X only. // // Normally on OS X we mount with the novncache option @@ -127,8 +131,14 @@ func Mount( return } + // Choose a parent context for ops. + opContext := config.OpContext + if opContext == nil { + opContext = context.Background() + } + // Create our own Connection object wrapping it. - connection, err := newConnection(logger, bfConn) + connection, err := newConnection(opContext, logger, bfConn) if err != nil { bfConn.Close() err = fmt.Errorf("newConnection: %v", err)