mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
2428880058
* Update go-libp2p to 0.12.0 go-libp2p 0.12.0 made some significant changes to the stream interfaces around stream closing: * Close now closes in both directions and frees the stream. However, unlike FullClose did, it doesn't _wait_ for the remote peer to respond with an EOF. * To close for writing, call CloseWrite (like one would on a TCP connection, etc.). This patch: * Replaces calls to FullClose with Close where appropriate. * Replaces calls to Close with CloseWrite where appropriate. * Removes redundant Close calls. * Calls Reset to where appropriate to indicate that the request/response was aborted. Unlike Close, this will not flush and will not cause the remote peer to read an EOF. Instead, the remote peer will read an ErrReset error. * Ensures we always either close or reset streams. Send wasn't closing the stream on some error paths. * Now that stream closing is async, we explicitly wait for a response when "hanging up" on a peer (so we don't hang up before they receive our response/goodbye message). * update bazel * Gazelle * revert unintentional bazel workspace change * appease an overzealous linter * update to latest * Refactor encoder * gazelle * Gazelle Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
|
"github.com/prysmaticlabs/prysm/shared/traceutil"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
// Send a message to a specific peer. The returned stream may be used for reading, but has been
|
|
// closed for writing.
|
|
//
|
|
// When done, the caller must Close or Reset on the stream.
|
|
func (s *Service) Send(ctx context.Context, message interface{}, baseTopic string, pid peer.ID) (network.Stream, error) {
|
|
ctx, span := trace.StartSpan(ctx, "p2p.Send")
|
|
defer span.End()
|
|
if err := VerifyTopicMapping(baseTopic, message); err != nil {
|
|
return nil, err
|
|
}
|
|
topic := baseTopic + s.Encoding().ProtocolSuffix()
|
|
span.AddAttributes(trace.StringAttribute("topic", topic))
|
|
|
|
// Apply max dial timeout when opening a new stream.
|
|
ctx, cancel := context.WithTimeout(ctx, maxDialTimeout)
|
|
defer cancel()
|
|
|
|
stream, err := s.host.NewStream(ctx, pid, protocol.ID(topic))
|
|
if err != nil {
|
|
traceutil.AnnotateError(span, err)
|
|
return nil, err
|
|
}
|
|
// do not encode anything if we are sending a metadata request
|
|
if baseTopic != RPCMetaDataTopic {
|
|
if _, err := s.Encoding().EncodeWithMaxLength(stream, message); err != nil {
|
|
traceutil.AnnotateError(span, err)
|
|
_err := stream.Reset()
|
|
_ = _err
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Close stream for writing.
|
|
if err := stream.CloseWrite(); err != nil {
|
|
traceutil.AnnotateError(span, err)
|
|
_err := stream.Reset()
|
|
_ = _err
|
|
return nil, err
|
|
}
|
|
|
|
return stream, nil
|
|
}
|