storageminer: Better context handling

This commit is contained in:
Łukasz Magiera 2019-09-17 20:36:06 +02:00
parent 037fa84e68
commit 5e2c100f4d
3 changed files with 10 additions and 43 deletions

View File

@ -21,7 +21,6 @@ var log = logging.Logger("cli")
const (
metadataTraceConetxt = "traceContext"
metadataContext = "context"
)
// ApiConnector returns API instance
@ -85,22 +84,19 @@ func GetStorageMinerAPI(ctx *cli.Context) (api.StorageMiner, error) {
return client.NewStorageMinerRPC(addr, headers)
}
func DaemonContext(cctx *cli.Context) context.Context {
if mtCtx, ok := cctx.App.Metadata[metadataTraceConetxt]; ok {
return mtCtx.(context.Context)
}
return context.Background()
}
// ReqContext returns context for cli execution. Calling it for the first time
// installs SIGTERM handler that will close returned context.
// Not safe for concurrent execution.
func ReqContext(cctx *cli.Context) context.Context {
if uctx, ok := cctx.App.Metadata[metadataContext]; ok {
// unchecked cast as if something else is in there
// it is crash worthy either way
return uctx.(context.Context)
}
var tCtx context.Context
if mtCtx, ok := cctx.App.Metadata[metadataTraceConetxt]; ok {
tCtx = mtCtx.(context.Context)
} else {
tCtx = context.Background()
}
tCtx := DaemonContext(cctx)
ctx, done := context.WithCancel(tCtx)
sigChan := make(chan os.Signal, 2)

View File

@ -2,7 +2,6 @@ package main
import (
"context"
"github.com/filecoin-project/go-lotus/lib/valctx"
"net/http"
"os"
"os/signal"
@ -36,7 +35,7 @@ var runCmd = &cli.Command{
if err != nil {
return err
}
ctx := &valctx.Context{Parent: lcli.ReqContext(cctx)}
ctx := lcli.DaemonContext(cctx)
v, err := nodeApi.Version(ctx)
if err != nil {

View File

@ -1,28 +0,0 @@
package valctx
import (
"context"
"time"
)
type Context struct {
Parent context.Context
}
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return
}
func (c *Context) Done() <-chan struct{} {
return nil
}
func (c *Context) Err() error {
return nil
}
func (c *Context) Value(key interface{}) interface{} {
return c.Parent.Value(key)
}
var _ context.Context = &Context{}