storageminer: Better context handling
This commit is contained in:
parent
037fa84e68
commit
5e2c100f4d
22
cli/cmd.go
22
cli/cmd.go
@ -21,7 +21,6 @@ var log = logging.Logger("cli")
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
metadataTraceConetxt = "traceContext"
|
metadataTraceConetxt = "traceContext"
|
||||||
metadataContext = "context"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ApiConnector returns API instance
|
// ApiConnector returns API instance
|
||||||
@ -85,22 +84,19 @@ func GetStorageMinerAPI(ctx *cli.Context) (api.StorageMiner, error) {
|
|||||||
return client.NewStorageMinerRPC(addr, headers)
|
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
|
// ReqContext returns context for cli execution. Calling it for the first time
|
||||||
// installs SIGTERM handler that will close returned context.
|
// installs SIGTERM handler that will close returned context.
|
||||||
// Not safe for concurrent execution.
|
// Not safe for concurrent execution.
|
||||||
func ReqContext(cctx *cli.Context) context.Context {
|
func ReqContext(cctx *cli.Context) context.Context {
|
||||||
if uctx, ok := cctx.App.Metadata[metadataContext]; ok {
|
tCtx := DaemonContext(cctx)
|
||||||
// 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()
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx, done := context.WithCancel(tCtx)
|
ctx, done := context.WithCancel(tCtx)
|
||||||
sigChan := make(chan os.Signal, 2)
|
sigChan := make(chan os.Signal, 2)
|
||||||
|
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/filecoin-project/go-lotus/lib/valctx"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@ -36,7 +35,7 @@ var runCmd = &cli.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx := &valctx.Context{Parent: lcli.ReqContext(cctx)}
|
ctx := lcli.DaemonContext(cctx)
|
||||||
|
|
||||||
v, err := nodeApi.Version(ctx)
|
v, err := nodeApi.Version(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -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{}
|
|
Loading…
Reference in New Issue
Block a user