jsonrpc: gofmt

This commit is contained in:
Łukasz Magiera 2019-07-22 22:28:40 +02:00
parent 790ed0730e
commit fd40f9a82a
5 changed files with 24 additions and 23 deletions

View File

@ -70,7 +70,7 @@ var Commands = []*cli.Command{
createMinerCmd, createMinerCmd,
{ {
Name:"testch", Name: "testch",
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
api, err := getAPI(cctx) api, err := getAPI(cctx)
if err != nil { if err != nil {
@ -87,7 +87,7 @@ var Commands = []*cli.Command{
select { select {
case n := <-c: case n := <-c:
fmt.Println(n) fmt.Println(n)
case <- ctx.Done(): case <-ctx.Done():
return nil return nil
} }
} }

View File

@ -4,6 +4,7 @@ package daemon
import ( import (
"context" "context"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"

View File

@ -34,12 +34,11 @@ func (e *ErrClient) Unwrap(err error) error {
return e.err return e.err
} }
type clientResponse struct { type clientResponse struct {
Jsonrpc string `json:"jsonrpc"` Jsonrpc string `json:"jsonrpc"`
Result json.RawMessage `json:"result"` Result json.RawMessage `json:"result"`
ID int64 `json:"id"` ID int64 `json:"id"`
Error *respError `json:"error,omitempty"` Error *respError `json:"error,omitempty"`
} }
type clientRequest struct { type clientRequest struct {

View File

@ -27,8 +27,8 @@ type frame struct {
Params []param `json:"params,omitempty"` Params []param `json:"params,omitempty"`
// response // response
Result json.RawMessage `json:"result,omitempty"` Result json.RawMessage `json:"result,omitempty"`
Error *respError `json:"error,omitempty"` Error *respError `json:"error,omitempty"`
} }
func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, requests <-chan clientRequest, stop <-chan struct{}) { 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 chOnce sync.Once
var outId uint64 var outId uint64
type chReg struct { id uint64; ch reflect.Value } type chReg struct {
id uint64
ch reflect.Value
}
registerCh := make(chan chReg) registerCh := make(chan chReg)
defer close(registerCh) defer close(registerCh)
@ -107,13 +110,12 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
cases := []reflect.SelectCase{ cases := []reflect.SelectCase{
{ // registration chan always 0 { // registration chan always 0
Dir: reflect.SelectRecv, Dir: reflect.SelectRecv,
Chan: regV, Chan: regV,
}, },
} }
var caseToId []uint64 var caseToId []uint64
for { for {
chosen, val, ok := reflect.Select(cases) 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) caseToId = append(caseToId, registration.id)
cases = append(cases, reflect.SelectCase{ cases = append(cases, reflect.SelectCase{
Dir: reflect.SelectRecv, Dir: reflect.SelectRecv,
Chan: registration.ch, Chan: registration.ch,
}) })
continue continue
} }
if !ok { if !ok {
n := len(caseToId) n := len(caseToId)
if n > 0 { if n > 0 {
cases[chosen] = cases[n] cases[chosen] = cases[n]
caseToId[chosen - 1] = caseToId[n - 1] caseToId[chosen-1] = caseToId[n-1]
} }
cases = cases[:n] cases = cases[:n]
@ -151,9 +152,9 @@ func handleWsConn(ctx context.Context, conn *websocket.Conn, handler handlers, r
sendReq(request{ sendReq(request{
Jsonrpc: "2.0", Jsonrpc: "2.0",
ID: nil, // notification ID: nil, // notification
Method: chValue, Method: chValue,
Params: []param{{v: reflect.ValueOf(caseToId[chosen - 1])}, {v: val}}, 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 { for id, req := range inflight {
req.ready <- clientResponse{ req.ready <- clientResponse{
Jsonrpc: "2.0", Jsonrpc: "2.0",
ID: id, ID: id,
Error: &respError{ Error: &respError{
Message: "handler: websocket connection closed", Message: "handler: websocket connection closed",
}, },

View File

@ -39,10 +39,10 @@ func (a *API) TestCh(ctx context.Context) (<-chan int, error) {
time.Sleep(time.Millisecond * 100) time.Sleep(time.Millisecond * 100)
n++ n++
select { select {
case <-ctx.Done(): case <-ctx.Done():
fmt.Println("CTXCANCEL!") fmt.Println("CTXCANCEL!")
return return
case out <- n: case out <- n:
} }
} }
}() }()