Move to Warnf
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
parent
2a350c6e82
commit
217cb247a5
@ -36,7 +36,7 @@ func makeSmuxTransportOption(mplexExp bool) libp2p.Option {
|
||||
for _, id := range order {
|
||||
tpt, ok := muxers[id]
|
||||
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
|
||||
}
|
||||
delete(muxers, id)
|
||||
|
@ -15,7 +15,7 @@ func Security(enabled, preferTLS bool) interface{} {
|
||||
if !enabled {
|
||||
return func() (opts Libp2pOpts) {
|
||||
// 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`)
|
||||
opts.Opts = append(opts.Opts, libp2p.NoSecurity)
|
||||
return opts
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -43,12 +42,15 @@ type clientResponse struct {
|
||||
Error *respError `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ClientCloser is used to close Client from further use
|
||||
type ClientCloser func()
|
||||
|
||||
// NewClient creates new josnrpc 2.0 client
|
||||
//
|
||||
// handler must be pointer to a struct with function fields
|
||||
//
|
||||
// Returned value closes the client from further use
|
||||
// TODO: Example
|
||||
func NewClient(addr string, namespace string, handler interface{}) {
|
||||
func NewClient(addr string, namespace string, handler interface{}) ClientCloser {
|
||||
htyp := reflect.TypeOf(handler)
|
||||
if htyp.Kind() != reflect.Ptr {
|
||||
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")
|
||||
}
|
||||
|
||||
closeChan := make(chan struct{})
|
||||
closer := func() {
|
||||
close(closeChan)
|
||||
}
|
||||
|
||||
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++ {
|
||||
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) {
|
||||
id := atomic.AddInt64(&idCtr, 1)
|
||||
id := <-ids
|
||||
params := make([]param, len(args)-hasCtx)
|
||||
for i, arg := range args[hasCtx:] {
|
||||
params[i] = param{
|
||||
@ -167,4 +193,5 @@ func NewClient(addr string, namespace string, handler interface{}) {
|
||||
|
||||
val.Elem().Field(i).Set(fn)
|
||||
}
|
||||
return closer
|
||||
}
|
||||
|
@ -70,7 +70,8 @@ func TestRPC(t *testing.T) {
|
||||
AddGet func(int) int
|
||||
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
|
||||
|
||||
@ -129,51 +130,56 @@ func TestRPC(t *testing.T) {
|
||||
var noret struct {
|
||||
Add func(int)
|
||||
}
|
||||
NewClient(testServ.URL, "SimpleServerHandler", &noret)
|
||||
closer = NewClient(testServ.URL, "SimpleServerHandler", &noret)
|
||||
|
||||
// this one should actually work
|
||||
noret.Add(4)
|
||||
if serverHandler.n != 9 {
|
||||
t.Error("expected 9")
|
||||
}
|
||||
closer()
|
||||
|
||||
var noparam struct {
|
||||
Add func()
|
||||
}
|
||||
NewClient(testServ.URL, "SimpleServerHandler", &noparam)
|
||||
closer = NewClient(testServ.URL, "SimpleServerHandler", &noparam)
|
||||
|
||||
// shouldn't panic
|
||||
noparam.Add()
|
||||
closer()
|
||||
|
||||
var erronly struct {
|
||||
AddGet func() (int, error)
|
||||
}
|
||||
NewClient(testServ.URL, "SimpleServerHandler", &erronly)
|
||||
closer = NewClient(testServ.URL, "SimpleServerHandler", &erronly)
|
||||
|
||||
_, err = erronly.AddGet()
|
||||
if err == nil || err.Error() != "RPC error (-32602): wrong param count" {
|
||||
t.Error("wrong error:", err)
|
||||
}
|
||||
closer()
|
||||
|
||||
var wrongtype struct {
|
||||
Add func(string) error
|
||||
}
|
||||
NewClient(testServ.URL, "SimpleServerHandler", &wrongtype)
|
||||
closer = NewClient(testServ.URL, "SimpleServerHandler", &wrongtype)
|
||||
|
||||
err = wrongtype.Add("not an int")
|
||||
if err == nil || err.Error() != "RPC error (-32700): json: cannot unmarshal string into Go value of type int" {
|
||||
t.Error("wrong error:", err)
|
||||
}
|
||||
closer()
|
||||
|
||||
var notfound struct {
|
||||
NotThere func(string) error
|
||||
}
|
||||
NewClient(testServ.URL, "SimpleServerHandler", ¬found)
|
||||
closer = NewClient(testServ.URL, "SimpleServerHandler", ¬found)
|
||||
|
||||
err = notfound.NotThere("hello?")
|
||||
if err == nil || err.Error() != "RPC error (-32601): method 'SimpleServerHandler.NotThere' not found" {
|
||||
t.Error("wrong error:", err)
|
||||
}
|
||||
closer()
|
||||
}
|
||||
|
||||
type CtxHandler struct {
|
||||
@ -213,7 +219,7 @@ func TestCtx(t *testing.T) {
|
||||
var client struct {
|
||||
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)
|
||||
defer cancel()
|
||||
@ -228,11 +234,12 @@ func TestCtx(t *testing.T) {
|
||||
serverHandler.cancelled = false
|
||||
|
||||
serverHandler.lk.Unlock()
|
||||
closer()
|
||||
|
||||
var noCtxClient struct {
|
||||
Test func()
|
||||
}
|
||||
NewClient(testServ.URL, "CtxHandler", &noCtxClient)
|
||||
closer = NewClient(testServ.URL, "CtxHandler", &noCtxClient)
|
||||
|
||||
noCtxClient.Test()
|
||||
|
||||
@ -243,4 +250,5 @@ func TestCtx(t *testing.T) {
|
||||
}
|
||||
|
||||
serverHandler.lk.Unlock()
|
||||
closer()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user