-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package mse | ||
|
||
import ( | ||
"context" | ||
g "github.com/anacrolix/generics" | ||
"io" | ||
) | ||
|
||
type contextedReader struct { | ||
ctx context.Context | ||
r io.Reader | ||
} | ||
|
||
func (me contextedReader) Read(p []byte) (n int, err error) { | ||
return contextedReadOrWrite(me.ctx, me.r.Read, p) | ||
} | ||
|
||
type contextedWriter struct { | ||
ctx context.Context | ||
w io.Writer | ||
} | ||
|
||
// This is problematic. If you return with a context error, a read or write is still pending, and | ||
// could mess up the stream. | ||
func contextedReadOrWrite(ctx context.Context, method func(b []byte) (int, error), b []byte) (_ int, err error) { | ||
asyncCh := make(chan g.Result[int], 1) | ||
go func() { | ||
asyncCh <- g.ResultFromTuple(method(b)) | ||
}() | ||
select { | ||
case <-ctx.Done(): | ||
err = context.Cause(ctx) | ||
return | ||
case res := <-asyncCh: | ||
return res.AsTuple() | ||
} | ||
|
||
} | ||
|
||
func (me contextedWriter) Write(p []byte) (n int, err error) { | ||
return contextedReadOrWrite(me.ctx, me.w.Write, p) | ||
} | ||
|
||
func contextedReadWriter(ctx context.Context, rw io.ReadWriter) io.ReadWriter { | ||
return struct { | ||
io.Reader | ||
io.Writer | ||
}{ | ||
contextedReader{ | ||
ctx: ctx, | ||
r: rw, | ||
}, | ||
contextedWriter{ | ||
ctx: ctx, | ||
w: rw, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.