jsonrpc: gofmt
This commit is contained in:
parent
790ed0730e
commit
fd40f9a82a
@ -70,7 +70,7 @@ var Commands = []*cli.Command{
|
||||
createMinerCmd,
|
||||
|
||||
{
|
||||
Name:"testch",
|
||||
Name: "testch",
|
||||
Action: func(cctx *cli.Context) error {
|
||||
api, err := getAPI(cctx)
|
||||
if err != nil {
|
||||
@ -87,7 +87,7 @@ var Commands = []*cli.Command{
|
||||
select {
|
||||
case n := <-c:
|
||||
fmt.Println(n)
|
||||
case <- ctx.Done():
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/multiformats/go-multiaddr"
|
||||
"gopkg.in/urfave/cli.v2"
|
||||
|
||||
|
@ -34,12 +34,11 @@ func (e *ErrClient) Unwrap(err error) error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
|
||||
type clientResponse struct {
|
||||
Jsonrpc string `json:"jsonrpc"`
|
||||
Result json.RawMessage `json:"result"`
|
||||
ID int64 `json:"id"`
|
||||
Error *respError `json:"error,omitempty"`
|
||||
Jsonrpc string `json:"jsonrpc"`
|
||||
Result json.RawMessage `json:"result"`
|
||||
ID int64 `json:"id"`
|
||||
Error *respError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type clientRequest struct {
|
||||
|
@ -27,8 +27,8 @@ type frame struct {
|
||||
Params []param `json:"params,omitempty"`
|
||||
|
||||
// response
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *respError `json:"error,omitempty"`
|
||||
Result json.RawMessage `json:"result,omitempty"`
|
||||
Error *respError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, requests <-chan clientRequest, stop <-chan struct{}) {
|
||||
@ -98,7 +98,10 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
|
||||
|
||||
var chOnce sync.Once
|
||||
var outId uint64
|
||||
type chReg struct { id uint64; ch reflect.Value }
|
||||
type chReg struct {
|
||||
id uint64
|
||||
ch reflect.Value
|
||||
}
|
||||
registerCh := make(chan chReg)
|
||||
defer close(registerCh)
|
||||
|
||||
@ -107,13 +110,12 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
|
||||
|
||||
cases := []reflect.SelectCase{
|
||||
{ // registration chan always 0
|
||||
Dir: reflect.SelectRecv,
|
||||
Dir: reflect.SelectRecv,
|
||||
Chan: regV,
|
||||
},
|
||||
}
|
||||
var caseToId []uint64
|
||||
|
||||
|
||||
for {
|
||||
chosen, val, ok := reflect.Select(cases)
|
||||
|
||||
@ -129,19 +131,18 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
|
||||
|
||||
caseToId = append(caseToId, registration.id)
|
||||
cases = append(cases, reflect.SelectCase{
|
||||
Dir: reflect.SelectRecv,
|
||||
Dir: reflect.SelectRecv,
|
||||
Chan: registration.ch,
|
||||
})
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
if !ok {
|
||||
n := len(caseToId)
|
||||
if n > 0 {
|
||||
cases[chosen] = cases[n]
|
||||
caseToId[chosen - 1] = caseToId[n - 1]
|
||||
caseToId[chosen-1] = caseToId[n-1]
|
||||
}
|
||||
|
||||
cases = cases[:n]
|
||||
@ -151,9 +152,9 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
|
||||
|
||||
sendReq(request{
|
||||
Jsonrpc: "2.0",
|
||||
ID: nil, // notification
|
||||
Method: chValue,
|
||||
Params: []param{{v: reflect.ValueOf(caseToId[chosen - 1])}, {v: val}},
|
||||
ID: nil, // notification
|
||||
Method: chValue,
|
||||
Params: []param{{v: reflect.ValueOf(caseToId[chosen-1])}, {v: val}},
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -184,7 +185,7 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
|
||||
for id, req := range inflight {
|
||||
req.ready <- clientResponse{
|
||||
Jsonrpc: "2.0",
|
||||
ID: id,
|
||||
ID: id,
|
||||
Error: &respError{
|
||||
Message: "handler: websocket connection closed",
|
||||
},
|
||||
|
@ -39,10 +39,10 @@ func (a *API) TestCh(ctx context.Context) (<-chan int, error) {
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
n++
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println("CTXCANCEL!")
|
||||
return
|
||||
case out <- n:
|
||||
case <-ctx.Done():
|
||||
fmt.Println("CTXCANCEL!")
|
||||
return
|
||||
case out <- n:
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
Loading…
Reference in New Issue
Block a user