Comments (1)
I figured it out after reading the release notes of libp2p 0.12.0:
This is release includes a significant BREAKING CHANGE to the stream interface.
Previously, Close() closed streams for writing, but left them open for reading. Unfortunately, this meant streams would not be garbage collected until either (a) an EOF had been read on the stream or (b) Reset had been called. While technically documented, this behavior was extraordinarily surprising and most libp2p applications end up misusing and leaking streams (leading to memory leaks).
In this release, Close now closes the stream for both reading and writing. Close will not wait for any form of acknowledgment. If acknowledgment is required, the caller must call CloseWrite (described below), then wait on the stream for a response (or an EOF), then call Close() to free the stream object.
To close the stream for writing only, the user should call CloseWrite(). Close write flushes any in-progress writes, then sends an EOF.
The user may now call CloseRead() to close the stream for reading only. CloseRead() will interrupt any in-progress reads with an error and prevent further reads from succeeding. At the protocol level, the behavior of CloseRead() is implementation defined.
Yamux and Mplex will throw away incoming data on a closed stream.
QUIC will return an error to the sender.
When done with a stream, the user must call either Close() or Reset() to discard the stream, even after calling CloseRead() and/or CloseWrite().
Now after I have written to the stream, I'm waiting for the remote peer to close/reset the connection:
func WaitForEOF(s network.Stream) error {
timeout := time.After(10 * time.Second)
done := make(chan error)
go func() {
buf := make([]byte, 1)
n, err := s.Read(buf)
if err == io.EOF && n == 0 {
err = nil
} else if n != 0 {
err = fmt.Errorf("stream returned data unexpectedly")
}
done <- err
close(done)
}()
select {
case <-timeout:
return fmt.Errorf("timeout")
case err := <-done:
return err
}
}
Closing of the stream is handled outside of WaitForEOF
.
from go-libp2p-core.
Related Issues (20)
- Host event bus does not emit EvtPeerConnectednessChanged HOT 5
- Error in using libp2pquic.NewTransport with libp2p.Transport HOT 2
- Record envelope protobuf does not match spec HOT 2
- Add a `ClearDeadline` API HOT 4
- v0.7.0 breaks backward compatibility for multiple packages HOT 3
- add WithStat option for host.NewStream HOT 3
- Unable to marshal / unmarshal AddrInfo within my struct HOT 4
- flaky TestResetBandwidthCounter test HOT 1
- How to know the conn has been closed
- What happened to the method in **helps**? HOT 1
- Reliable Notifiee events HOT 1
- looks unsafe HOT 2
- routing.go HOT 1
- How to convert PrivKey to crypto/ecdsa.PrivateKey? HOT 1
- Requesting release 0.16 with "update btcec dependency" change HOT 4
- btcec update leads to `go mod tidy` failure HOT 9
- extend `peer.Set` with the Remove method HOT 4
- Unable to know data on the connected node HOT 1
- use a mock clock in bandwidth counter tests HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-libp2p-core.