Move to Warnf

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-03 14:30:21 +02:00
parent 2a350c6e82
commit 217cb247a5
4 changed files with 50 additions and 15 deletions

View File

@ -36,7 +36,7 @@ func makeSmuxTransportOption(mplexExp bool) libp2p.Option {
for _, id := range order { for _, id := range order {
tpt, ok := muxers[id] tpt, ok := muxers[id]
if !ok { if !ok {
log.Warningf("unknown or duplicate muxer in LIBP2P_MUX_PREFS: %s", id) log.Warnf("unknown or duplicate muxer in LIBP2P_MUX_PREFS: %s", id)
continue continue
} }
delete(muxers, id) delete(muxers, id)

View File

@ -15,7 +15,7 @@ func Security(enabled, preferTLS bool) interface{} {
if !enabled { if !enabled {
return func() (opts Libp2pOpts) { return func() (opts Libp2pOpts) {
// TODO: shouldn't this be Errorf to guarantee visibility? // TODO: shouldn't this be Errorf to guarantee visibility?
log.Warningf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS. log.Warnf(`Your IPFS node has been configured to run WITHOUT ENCRYPTED CONNECTIONS.
You will not be able to connect to any nodes configured to use encrypted connections`) You will not be able to connect to any nodes configured to use encrypted connections`)
opts.Opts = append(opts.Opts, libp2p.NoSecurity) opts.Opts = append(opts.Opts, libp2p.NoSecurity)
return opts return opts

View File

@ -8,7 +8,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"reflect" "reflect"
"sync/atomic"
) )
var ( var (
@ -43,12 +42,15 @@ type clientResponse struct {
Error *respError `json:"error,omitempty"` Error *respError `json:"error,omitempty"`
} }
// ClientCloser is used to close Client from further use
type ClientCloser func()
// NewClient creates new josnrpc 2.0 client // NewClient creates new josnrpc 2.0 client
// //
// handler must be pointer to a struct with function fields // handler must be pointer to a struct with function fields
// // Returned value closes the client from further use
// TODO: Example // TODO: Example
func NewClient(addr string, namespace string, handler interface{}) { func NewClient(addr string, namespace string, handler interface{}) ClientCloser {
htyp := reflect.TypeOf(handler) htyp := reflect.TypeOf(handler)
if htyp.Kind() != reflect.Ptr { if htyp.Kind() != reflect.Ptr {
panic("expected handler to be a pointer") panic("expected handler to be a pointer")
@ -58,9 +60,33 @@ func NewClient(addr string, namespace string, handler interface{}) {
panic("handler should be a struct") panic("handler should be a struct")
} }
closeChan := make(chan struct{})
closer := func() {
close(closeChan)
}
val := reflect.ValueOf(handler) val := reflect.ValueOf(handler)
var idCtr int64 var ids <-chan int64
{
idsChan := make(chan int64, 64)
go func() {
var id int64
for {
select {
case idsChan <- id:
id++
case _, ok := <-closeChan:
if !ok {
return
}
}
}
}()
ids = idsChan
}
for i := 0; i < typ.NumField(); i++ { for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i) f := typ.Field(i)
@ -107,7 +133,7 @@ func NewClient(addr string, namespace string, handler interface{}) {
} }
fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) { fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) {
id := atomic.AddInt64(&idCtr, 1) id := <-ids
params := make([]param, len(args)-hasCtx) params := make([]param, len(args)-hasCtx)
for i, arg := range args[hasCtx:] { for i, arg := range args[hasCtx:] {
params[i] = param{ params[i] = param{
@ -167,4 +193,5 @@ func NewClient(addr string, namespace string, handler interface{}) {
val.Elem().Field(i).Set(fn) val.Elem().Field(i).Set(fn)
} }
return closer
} }

View File

@ -70,7 +70,8 @@ func TestRPC(t *testing.T) {
AddGet func(int) int AddGet func(int) int
StringMatch func(t TestType, i2 int64) (out TestOut, err error) StringMatch func(t TestType, i2 int64) (out TestOut, err error)
} }
NewClient(testServ.URL, "SimpleServerHandler", &client) closer := NewClient(testServ.URL, "SimpleServerHandler", &client)
defer closer()
// Add(int) error // Add(int) error
@ -129,51 +130,56 @@ func TestRPC(t *testing.T) {
var noret struct { var noret struct {
Add func(int) Add func(int)
} }
NewClient(testServ.URL, "SimpleServerHandler", &noret) closer = NewClient(testServ.URL, "SimpleServerHandler", &noret)
// this one should actually work // this one should actually work
noret.Add(4) noret.Add(4)
if serverHandler.n != 9 { if serverHandler.n != 9 {
t.Error("expected 9") t.Error("expected 9")
} }
closer()
var noparam struct { var noparam struct {
Add func() Add func()
} }
NewClient(testServ.URL, "SimpleServerHandler", &noparam) closer = NewClient(testServ.URL, "SimpleServerHandler", &noparam)
// shouldn't panic // shouldn't panic
noparam.Add() noparam.Add()
closer()
var erronly struct { var erronly struct {
AddGet func() (int, error) AddGet func() (int, error)
} }
NewClient(testServ.URL, "SimpleServerHandler", &erronly) closer = NewClient(testServ.URL, "SimpleServerHandler", &erronly)
_, err = erronly.AddGet() _, err = erronly.AddGet()
if err == nil || err.Error() != "RPC error (-32602): wrong param count" { if err == nil || err.Error() != "RPC error (-32602): wrong param count" {
t.Error("wrong error:", err) t.Error("wrong error:", err)
} }
closer()
var wrongtype struct { var wrongtype struct {
Add func(string) error Add func(string) error
} }
NewClient(testServ.URL, "SimpleServerHandler", &wrongtype) closer = NewClient(testServ.URL, "SimpleServerHandler", &wrongtype)
err = wrongtype.Add("not an int") err = wrongtype.Add("not an int")
if err == nil || err.Error() != "RPC error (-32700): json: cannot unmarshal string into Go value of type int" { if err == nil || err.Error() != "RPC error (-32700): json: cannot unmarshal string into Go value of type int" {
t.Error("wrong error:", err) t.Error("wrong error:", err)
} }
closer()
var notfound struct { var notfound struct {
NotThere func(string) error NotThere func(string) error
} }
NewClient(testServ.URL, "SimpleServerHandler", &notfound) closer = NewClient(testServ.URL, "SimpleServerHandler", &notfound)
err = notfound.NotThere("hello?") err = notfound.NotThere("hello?")
if err == nil || err.Error() != "RPC error (-32601): method 'SimpleServerHandler.NotThere' not found" { if err == nil || err.Error() != "RPC error (-32601): method 'SimpleServerHandler.NotThere' not found" {
t.Error("wrong error:", err) t.Error("wrong error:", err)
} }
closer()
} }
type CtxHandler struct { type CtxHandler struct {
@ -213,7 +219,7 @@ func TestCtx(t *testing.T) {
var client struct { var client struct {
Test func(ctx context.Context) Test func(ctx context.Context)
} }
NewClient(testServ.URL, "CtxHandler", &client) closer := NewClient(testServ.URL, "CtxHandler", &client)
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond) ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel() defer cancel()
@ -228,11 +234,12 @@ func TestCtx(t *testing.T) {
serverHandler.cancelled = false serverHandler.cancelled = false
serverHandler.lk.Unlock() serverHandler.lk.Unlock()
closer()
var noCtxClient struct { var noCtxClient struct {
Test func() Test func()
} }
NewClient(testServ.URL, "CtxHandler", &noCtxClient) closer = NewClient(testServ.URL, "CtxHandler", &noCtxClient)
noCtxClient.Test() noCtxClient.Test()
@ -243,4 +250,5 @@ func TestCtx(t *testing.T) {
} }
serverHandler.lk.Unlock() serverHandler.lk.Unlock()
closer()
} }