Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

fix closing of streams in example #221

Merged
merged 1 commit into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os"

ic "github.com/libp2p/go-libp2p-core/crypto"
peer "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peer"
libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
ma "github.com/multiformats/go-multiaddr"
)
Expand Down Expand Up @@ -53,12 +53,13 @@ func run(raddr string, p string) error {
if err != nil {
return err
}
defer str.Close()
const msg = "Hello world!"
log.Printf("Sending: %s\n", msg)
if _, err := str.Write([]byte(msg)); err != nil {
return err
}
if err := str.Close(); err != nil {
if err := str.CloseWrite(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to call close eventually (e.g., in a defer).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could, but streams are automatically closed when the connection is closed, so this is not necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, this is an example, and we should probably do things the clean way. Will add the defers.

return err
}
data, err := ioutil.ReadAll(str)
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func handleConn(conn tpt.CapableConn) error {
return err
}
log.Printf("Received: %s\n", data)
if _, err := str.Write([]byte(data)); err != nil {
if _, err := str.Write(data); err != nil {
return err
}
return str.Close()
Expand Down