stream ctx + peer addr
This commit is contained in:
parent
ed0a751d15
commit
39a8c59f83
@ -51,6 +51,10 @@ func (p pushSpaceRequestMatcher) String() string {
|
|||||||
|
|
||||||
type mockPeer struct{}
|
type mockPeer struct{}
|
||||||
|
|
||||||
|
func (m mockPeer) Addr() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (m mockPeer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
func (m mockPeer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
||||||
return true, m.Close()
|
return true, m.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
1
metric/log_test.go
Normal file
1
metric/log_test.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package metric
|
||||||
@ -13,6 +13,7 @@ type contextKey uint
|
|||||||
const (
|
const (
|
||||||
contextKeyPeerId contextKey = iota
|
contextKeyPeerId contextKey = iota
|
||||||
contextKeyIdentity
|
contextKeyIdentity
|
||||||
|
contextKeyPeerAddr
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -36,6 +37,19 @@ func CtxWithPeerId(ctx context.Context, peerId string) context.Context {
|
|||||||
return context.WithValue(ctx, contextKeyPeerId, peerId)
|
return context.WithValue(ctx, contextKeyPeerId, peerId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CtxPeerAddr returns peer address
|
||||||
|
func CtxPeerAddr(ctx context.Context) string {
|
||||||
|
if p, ok := ctx.Value(contextKeyPeerAddr).(string); ok {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// CtxWithPeerAddr sets peer address to the context
|
||||||
|
func CtxWithPeerAddr(ctx context.Context, addr string) context.Context {
|
||||||
|
return context.WithValue(ctx, contextKeyPeerAddr, addr)
|
||||||
|
}
|
||||||
|
|
||||||
// CtxIdentity returns identity from context
|
// CtxIdentity returns identity from context
|
||||||
func CtxIdentity(ctx context.Context) ([]byte, error) {
|
func CtxIdentity(ctx context.Context) ([]byte, error) {
|
||||||
if identity, ok := ctx.Value(contextKeyIdentity).([]byte); ok {
|
if identity, ok := ctx.Value(contextKeyIdentity).([]byte); ok {
|
||||||
|
|||||||
@ -26,6 +26,7 @@ type Peer interface {
|
|||||||
Id() string
|
Id() string
|
||||||
LastUsage() time.Time
|
LastUsage() time.Time
|
||||||
UpdateLastUsage()
|
UpdateLastUsage()
|
||||||
|
Addr() string
|
||||||
TryClose(objectTTL time.Duration) (res bool, err error)
|
TryClose(objectTTL time.Duration) (res bool, err error)
|
||||||
drpc.Conn
|
drpc.Conn
|
||||||
}
|
}
|
||||||
@ -86,6 +87,13 @@ func (p *peer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
|||||||
return true, p.Close()
|
return true, p.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *peer) Addr() string {
|
||||||
|
if p.sc != nil {
|
||||||
|
return p.sc.RemoteAddr().String()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (p *peer) Close() (err error) {
|
func (p *peer) Close() (err error) {
|
||||||
log.Debug("peer close", zap.String("peerId", p.id))
|
log.Debug("peer close", zap.String("peerId", p.id))
|
||||||
return p.Conn.Close()
|
return p.Conn.Close()
|
||||||
|
|||||||
@ -184,6 +184,10 @@ type testPeer struct {
|
|||||||
closed chan struct{}
|
closed chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *testPeer) Addr() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (t *testPeer) Id() string {
|
func (t *testPeer) Id() string {
|
||||||
return t.id
|
return t.id
|
||||||
}
|
}
|
||||||
|
|||||||
@ -103,6 +103,10 @@ type testPeer struct {
|
|||||||
drpc.Conn
|
drpc.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t testPeer) Addr() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func (t testPeer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
func (t testPeer) TryClose(objectTTL time.Duration) (res bool, err error) {
|
||||||
return true, t.Close()
|
return true, t.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/anytypeio/any-sync/net/peer"
|
||||||
"github.com/anytypeio/any-sync/net/secureservice"
|
"github.com/anytypeio/any-sync/net/secureservice"
|
||||||
"github.com/libp2p/go-libp2p/core/sec"
|
"github.com/libp2p/go-libp2p/core/sec"
|
||||||
"github.com/zeebo/errs"
|
"github.com/zeebo/errs"
|
||||||
@ -98,8 +99,11 @@ func (s *BaseDrpcServer) serveConn(conn net.Conn) {
|
|||||||
l.Info("handshake error", zap.Error(err))
|
l.Info("handshake error", zap.Error(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if sc, ok := conn.(sec.SecureConn); ok {
|
||||||
|
ctx = peer.CtxWithPeerId(ctx, sc.RemotePeer().String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
ctx = peer.CtxWithPeerAddr(ctx, conn.RemoteAddr().String())
|
||||||
l.Debug("connection opened")
|
l.Debug("connection opened")
|
||||||
if err := s.drpcServer.ServeOne(ctx, conn); err != nil {
|
if err := s.drpcServer.ServeOne(ctx, conn); err != nil {
|
||||||
if errs.Is(err, context.Canceled) || errs.Is(err, io.EOF) {
|
if errs.Is(err, context.Canceled) || errs.Is(err, io.EOF) {
|
||||||
|
|||||||
@ -10,6 +10,7 @@ import (
|
|||||||
|
|
||||||
type stream struct {
|
type stream struct {
|
||||||
peerId string
|
peerId string
|
||||||
|
peerCtx context.Context
|
||||||
stream drpc.Stream
|
stream drpc.Stream
|
||||||
pool *streamPool
|
pool *streamPool
|
||||||
streamId uint32
|
streamId uint32
|
||||||
@ -36,7 +37,7 @@ func (sr *stream) readLoop() error {
|
|||||||
sr.l.Info("msg receive error", zap.Error(err))
|
sr.l.Info("msg receive error", zap.Error(err))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx := streamCtx(context.Background(), sr.streamId, sr.peerId)
|
ctx := streamCtx(sr.peerCtx, sr.streamId, sr.peerId)
|
||||||
ctx = logger.CtxWithFields(ctx, zap.String("peerId", sr.peerId))
|
ctx = logger.CtxWithFields(ctx, zap.String("peerId", sr.peerId))
|
||||||
if err := sr.pool.handler.HandleMessage(ctx, sr.peerId, msg); err != nil {
|
if err := sr.pool.handler.HandleMessage(ctx, sr.peerId, msg); err != nil {
|
||||||
sr.l.Info("msg handle error", zap.Error(err))
|
sr.l.Info("msg handle error", zap.Error(err))
|
||||||
|
|||||||
@ -27,9 +27,9 @@ type PeerGetter func(ctx context.Context) (peers []peer.Peer, err error)
|
|||||||
// StreamPool keeps and read streams
|
// StreamPool keeps and read streams
|
||||||
type StreamPool interface {
|
type StreamPool interface {
|
||||||
// AddStream adds new outgoing stream into the pool
|
// AddStream adds new outgoing stream into the pool
|
||||||
AddStream(peerId string, stream drpc.Stream, tags ...string)
|
AddStream(stream drpc.Stream, tags ...string) (err error)
|
||||||
// ReadStream adds new incoming stream and synchronously read it
|
// ReadStream adds new incoming stream and synchronously read it
|
||||||
ReadStream(peerId string, stream drpc.Stream, tags ...string) (err error)
|
ReadStream(stream drpc.Stream, tags ...string) (err error)
|
||||||
// Send sends a message to given peers. A stream will be opened if it is not cached before. Works async.
|
// Send sends a message to given peers. A stream will be opened if it is not cached before. Works async.
|
||||||
Send(ctx context.Context, msg drpc.Message, target PeerGetter) (err error)
|
Send(ctx context.Context, msg drpc.Message, target PeerGetter) (err error)
|
||||||
// SendById sends a message to given peerIds. Works only if stream exists
|
// SendById sends a message to given peerIds. Works only if stream exists
|
||||||
@ -63,16 +63,23 @@ type openingProcess struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamPool) ReadStream(peerId string, drpcStream drpc.Stream, tags ...string) error {
|
func (s *streamPool) ReadStream(drpcStream drpc.Stream, tags ...string) error {
|
||||||
st := s.addStream(peerId, drpcStream, tags...)
|
st, err := s.addStream(drpcStream, tags...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return st.readLoop()
|
return st.readLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamPool) AddStream(peerId string, drpcStream drpc.Stream, tags ...string) {
|
func (s *streamPool) AddStream(drpcStream drpc.Stream, tags ...string) error {
|
||||||
st := s.addStream(peerId, drpcStream, tags...)
|
st, err := s.addStream(drpcStream, tags...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
go func() {
|
go func() {
|
||||||
_ = st.readLoop()
|
_ = st.readLoop()
|
||||||
}()
|
}()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamPool) Streams(tags ...string) (streams []drpc.Stream) {
|
func (s *streamPool) Streams(tags ...string) (streams []drpc.Stream) {
|
||||||
@ -86,13 +93,19 @@ func (s *streamPool) Streams(tags ...string) (streams []drpc.Stream) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamPool) addStream(peerId string, drpcStream drpc.Stream, tags ...string) *stream {
|
func (s *streamPool) addStream(drpcStream drpc.Stream, tags ...string) (*stream, error) {
|
||||||
|
ctx := drpcStream.Context()
|
||||||
|
peerId, err := peer.CtxPeerId(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
s.lastStreamId++
|
s.lastStreamId++
|
||||||
streamId := s.lastStreamId
|
streamId := s.lastStreamId
|
||||||
st := &stream{
|
st := &stream{
|
||||||
peerId: peerId,
|
peerId: peerId,
|
||||||
|
peerCtx: ctx,
|
||||||
stream: drpcStream,
|
stream: drpcStream,
|
||||||
pool: s,
|
pool: s,
|
||||||
streamId: streamId,
|
streamId: streamId,
|
||||||
@ -104,7 +117,7 @@ func (s *streamPool) addStream(peerId string, drpcStream drpc.Stream, tags ...st
|
|||||||
for _, tag := range tags {
|
for _, tag := range tags {
|
||||||
s.streamIdsByTag[tag] = append(s.streamIdsByTag[tag], streamId)
|
s.streamIdsByTag[tag] = append(s.streamIdsByTag[tag], streamId)
|
||||||
}
|
}
|
||||||
return st
|
return st, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *streamPool) Send(ctx context.Context, msg drpc.Message, peerGetter PeerGetter) (err error) {
|
func (s *streamPool) Send(ctx context.Context, msg drpc.Message, peerGetter PeerGetter) (err error) {
|
||||||
@ -241,7 +254,10 @@ func (s *streamPool) openStream(ctx context.Context, p peer.Peer) *openingProces
|
|||||||
op.err = err
|
op.err = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.AddStream(p.Id(), st, tags...)
|
if err = s.AddStream(st, tags...); err != nil {
|
||||||
|
op.err = nil
|
||||||
|
return
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
return op
|
return op
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,7 @@ var ctx = context.Background()
|
|||||||
func newClientStream(t *testing.T, fx *fixture, peerId string) (st testservice.DRPCTest_TestStreamClient, p peer.Peer) {
|
func newClientStream(t *testing.T, fx *fixture, peerId string) (st testservice.DRPCTest_TestStreamClient, p peer.Peer) {
|
||||||
p, err := fx.tp.Dial(ctx, peerId)
|
p, err := fx.tp.Dial(ctx, peerId)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
ctx = peer.CtxWithPeerId(ctx, peerId)
|
||||||
s, err := testservice.NewDRPCTestClient(p).TestStream(ctx)
|
s, err := testservice.NewDRPCTestClient(p).TestStream(ctx)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return s, p
|
return s, p
|
||||||
@ -33,9 +34,9 @@ func TestStreamPool_AddStream(t *testing.T) {
|
|||||||
defer fx.Finish(t)
|
defer fx.Finish(t)
|
||||||
|
|
||||||
s1, _ := newClientStream(t, fx, "p1")
|
s1, _ := newClientStream(t, fx, "p1")
|
||||||
fx.AddStream("p1", s1, "space1", "common")
|
require.NoError(t, fx.AddStream(s1, "space1", "common"))
|
||||||
s2, _ := newClientStream(t, fx, "p2")
|
s2, _ := newClientStream(t, fx, "p2")
|
||||||
fx.AddStream("p2", s2, "space2", "common")
|
require.NoError(t, fx.AddStream(s2, "space2", "common"))
|
||||||
|
|
||||||
require.NoError(t, fx.Broadcast(ctx, &testservice.StreamMessage{ReqData: "space1"}, "space1"))
|
require.NoError(t, fx.Broadcast(ctx, &testservice.StreamMessage{ReqData: "space1"}, "space1"))
|
||||||
require.NoError(t, fx.Broadcast(ctx, &testservice.StreamMessage{ReqData: "space2"}, "space2"))
|
require.NoError(t, fx.Broadcast(ctx, &testservice.StreamMessage{ReqData: "space2"}, "space2"))
|
||||||
@ -64,7 +65,7 @@ func TestStreamPool_AddStream(t *testing.T) {
|
|||||||
|
|
||||||
s1, p1 := newClientStream(t, fx, "p1")
|
s1, p1 := newClientStream(t, fx, "p1")
|
||||||
defer s1.Close()
|
defer s1.Close()
|
||||||
fx.AddStream("p1", s1, "space1", "common")
|
require.NoError(t, fx.AddStream(s1, "space1", "common"))
|
||||||
|
|
||||||
require.NoError(t, fx.Send(ctx, &testservice.StreamMessage{ReqData: "test"}, func(ctx context.Context) (peers []peer.Peer, err error) {
|
require.NoError(t, fx.Send(ctx, &testservice.StreamMessage{ReqData: "test"}, func(ctx context.Context) (peers []peer.Peer, err error) {
|
||||||
return []peer.Peer{p1}, nil
|
return []peer.Peer{p1}, nil
|
||||||
@ -159,7 +160,7 @@ func TestStreamPool_SendById(t *testing.T) {
|
|||||||
|
|
||||||
s1, _ := newClientStream(t, fx, "p1")
|
s1, _ := newClientStream(t, fx, "p1")
|
||||||
defer s1.Close()
|
defer s1.Close()
|
||||||
fx.AddStream("p1", s1, "space1", "common")
|
require.NoError(t, fx.AddStream(s1, "space1", "common"))
|
||||||
|
|
||||||
require.NoError(t, fx.SendById(ctx, &testservice.StreamMessage{ReqData: "test"}, "p1"))
|
require.NoError(t, fx.SendById(ctx, &testservice.StreamMessage{ReqData: "test"}, "p1"))
|
||||||
var msg *testservice.StreamMessage
|
var msg *testservice.StreamMessage
|
||||||
@ -177,11 +178,11 @@ func TestStreamPool_Tags(t *testing.T) {
|
|||||||
|
|
||||||
s1, _ := newClientStream(t, fx, "p1")
|
s1, _ := newClientStream(t, fx, "p1")
|
||||||
defer s1.Close()
|
defer s1.Close()
|
||||||
fx.AddStream("p1", s1, "t1")
|
require.NoError(t, fx.AddStream(s1, "t1"))
|
||||||
|
|
||||||
s2, _ := newClientStream(t, fx, "p2")
|
s2, _ := newClientStream(t, fx, "p2")
|
||||||
defer s1.Close()
|
defer s1.Close()
|
||||||
fx.AddStream("p2", s2, "t2")
|
require.NoError(t, fx.AddStream(s2, "t2"))
|
||||||
|
|
||||||
err := fx.AddTagsCtx(streamCtx(ctx, 1, "p1"), "t3", "t3")
|
err := fx.AddTagsCtx(streamCtx(ctx, 1, "p1"), "t3", "t3")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user