Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
mybee
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
mybee
Commits
7118b9f7
Commit
7118b9f7
authored
Jan 29, 2020
by
Janos Guljas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add debug api peer disconnect handler
parent
8779acba
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
1 deletion
+70
-1
peer.go
pkg/debugapi/peer.go
+19
-0
peer_test.go
pkg/debugapi/peer_test.go
+43
-0
router.go
pkg/debugapi/router.go
+3
-0
libp2p.go
pkg/p2p/libp2p/libp2p.go
+5
-1
No files found.
pkg/debugapi/peer.go
View file @
7118b9f7
...
...
@@ -5,9 +5,11 @@
package
debugapi
import
(
"errors"
"net/http"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/gorilla/mux"
"github.com/multiformats/go-multiaddr"
)
...
...
@@ -36,3 +38,20 @@ func (s *server) peerConnectHandler(w http.ResponseWriter, r *http.Request) {
Address
:
address
,
})
}
func
(
s
*
server
)
peerDisconnectHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
addr
:=
mux
.
Vars
(
r
)[
"address"
]
if
err
:=
s
.
P2P
.
Disconnect
(
addr
);
err
!=
nil
{
s
.
Logger
.
Debugf
(
"debug api: peer disconnect %s: %v"
,
addr
,
err
)
if
errors
.
Is
(
err
,
p2p
.
ErrPeerNotFound
)
{
jsonhttp
.
BadRequest
(
w
,
"peer not found"
)
return
}
s
.
Logger
.
Errorf
(
"unable to disconnect peer %s"
,
addr
)
jsonhttp
.
InternalServerError
(
w
,
err
)
return
}
jsonhttp
.
OK
(
w
,
nil
)
}
pkg/debugapi/peer_test.go
View file @
7118b9f7
...
...
@@ -13,6 +13,7 @@ import (
"github.com/ethersphere/bee/pkg/debugapi"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/jsonhttp/jsonhttptest"
"github.com/ethersphere/bee/pkg/p2p"
"github.com/ethersphere/bee/pkg/p2p/mock"
ma
"github.com/multiformats/go-multiaddr"
)
...
...
@@ -53,3 +54,45 @@ func TestConnect(t *testing.T) {
})
})
}
func
TestDisconnect
(
t
*
testing
.
T
)
{
address
:=
"985732527402"
unknwonAdddress
:=
"123456"
errorAddress
:=
"33333333"
testErr
:=
errors
.
New
(
"test error"
)
client
,
cleanup
:=
newTestServer
(
t
,
testServerOptions
{
P2P
:
mock
.
New
(
mock
.
WithDisconnectFunc
(
func
(
addr
string
)
error
{
switch
addr
{
case
address
:
return
nil
case
errorAddress
:
return
testErr
default
:
return
p2p
.
ErrPeerNotFound
}
})),
})
defer
cleanup
()
t
.
Run
(
"ok"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodDelete
,
"/peers/"
+
address
,
nil
,
http
.
StatusOK
,
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusOK
,
Message
:
http
.
StatusText
(
http
.
StatusOK
),
})
})
t
.
Run
(
"unknown"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodDelete
,
"/peers/"
+
unknwonAdddress
,
nil
,
http
.
StatusBadRequest
,
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusBadRequest
,
Message
:
"peer not found"
,
})
})
t
.
Run
(
"error"
,
func
(
t
*
testing
.
T
)
{
jsonhttptest
.
ResponseDirect
(
t
,
client
,
http
.
MethodDelete
,
"/peers/"
+
errorAddress
,
nil
,
http
.
StatusInternalServerError
,
jsonhttp
.
StatusResponse
{
Code
:
http
.
StatusInternalServerError
,
Message
:
testErr
.
Error
(),
})
})
}
pkg/debugapi/router.go
View file @
7118b9f7
...
...
@@ -49,6 +49,9 @@ func (s *server) setupRouting() {
internalRouter
.
Handle
(
"/connect/{multi-address:.+}"
,
jsonhttp
.
MethodHandler
{
"POST"
:
http
.
HandlerFunc
(
s
.
peerConnectHandler
),
})
internalRouter
.
Handle
(
"/peers/{address}"
,
jsonhttp
.
MethodHandler
{
"DELETE"
:
http
.
HandlerFunc
(
s
.
peerDisconnectHandler
),
})
s
.
Handler
=
internalBaseRouter
}
pkg/p2p/libp2p/libp2p.go
View file @
7118b9f7
...
...
@@ -334,7 +334,11 @@ func (s *Service) Disconnect(overlay string) error {
if
!
found
{
return
p2p
.
ErrPeerNotFound
}
return
s
.
host
.
Network
()
.
ClosePeer
(
peerID
)
if
err
:=
s
.
host
.
Network
()
.
ClosePeer
(
peerID
);
err
!=
nil
{
return
err
}
s
.
peers
.
remove
(
peerID
)
return
nil
}
func
(
s
*
Service
)
NewStream
(
ctx
context
.
Context
,
overlay
,
protocolName
,
streamName
,
version
string
)
(
p2p
.
Stream
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment