You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
453 B
29 lines
453 B
package ctxreader |
|
|
|
import ( |
|
"context" |
|
"io" |
|
) |
|
|
|
type ContextReader struct { |
|
ctx context.Context |
|
r io.ReadCloser |
|
} |
|
|
|
func (cr *ContextReader) Read(p []byte) (n int, err error) { |
|
if err := cr.ctx.Err(); err != nil { |
|
return 0, err |
|
} |
|
return cr.r.Read(p) |
|
} |
|
|
|
func (cr *ContextReader) Close() error { |
|
return cr.r.Close() |
|
} |
|
|
|
func NewContextReader(ctx context.Context, r io.ReadCloser) io.ReadCloser { |
|
return &ContextReader{ |
|
ctx: ctx, |
|
r: r, |
|
} |
|
}
|
|
|