Skip to content

Commit fb0f0cb

Browse files
committed
conditionally drain channel in writerchan
1 parent 95cae0c commit fb0f0cb

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

pkg/util/iochan/iochan.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import (
2020
// ReaderChan reads from an io.Reader and sends the data to a channel
2121
func ReaderChan(ctx context.Context, r io.Reader, chunkSize int64, callback func()) chan wshrpc.RespOrErrorUnion[iochantypes.Packet] {
2222
ch := make(chan wshrpc.RespOrErrorUnion[iochantypes.Packet], 32)
23-
sha256Hash := sha256.New()
2423
go func() {
2524
defer func() {
2625
close(ch)
2726
callback()
2827
}()
28+
sha256Hash := sha256.New()
2929
for {
3030
select {
3131
case <-ctx.Done():
@@ -57,12 +57,14 @@ func ReaderChan(ctx context.Context, r io.Reader, chunkSize int64, callback func
5757

5858
// WriterChan reads from a channel and writes the data to an io.Writer
5959
func WriterChan(ctx context.Context, w io.Writer, ch <-chan wshrpc.RespOrErrorUnion[iochantypes.Packet], callback func(), cancel context.CancelCauseFunc) {
60-
sha256Hash := sha256.New()
6160
go func() {
6261
defer func() {
63-
drainChannel(ch)
62+
if ctx.Err() != nil {
63+
drainChannel(ch)
64+
}
6465
callback()
6566
}()
67+
sha256Hash := sha256.New()
6668
for {
6769
select {
6870
case <-ctx.Done():
@@ -97,6 +99,8 @@ func WriterChan(ctx context.Context, w io.Writer, ch <-chan wshrpc.RespOrErrorUn
9799
}
98100

99101
func drainChannel(ch <-chan wshrpc.RespOrErrorUnion[iochantypes.Packet]) {
100-
for range ch {
101-
}
102+
go func() {
103+
for range ch {
104+
}
105+
}()
102106
}

0 commit comments

Comments
 (0)