Allow setting a parent context for all ops.

geesefs-0-30-9
Aaron Jacobs 2015-05-01 11:17:15 +10:00
parent a76742833c
commit 0985694a4b
4 changed files with 30 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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)