mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-23 03:17:43 +08:00
- updated go-ctxgroup and goprocess ctxgroup: AddChildGroup was changed to AddChild. Used in two files: - p2p/net/mock/mock_net.go - routing/dht/dht.go - updated context from hg repo to git prev. commit in hg was ad01a6fcc8a19d3a4478c836895ffe883bd2ceab. (context: make parentCancelCtx iterative) represents commit 84f8955a887232b6308d79c68b8db44f64df455c in git repo - updated context to master (b6fdb7d8a4ccefede406f8fe0f017fb58265054c) Aaron Jacobs (2): net/context: Don't accept a context in the DoSomethingSlow example. context: Be clear that users must cancel the result of WithCancel. Andrew Gerrand (1): go.net: use golang.org/x/... import paths Bryan C. Mills (1): net/context: Don't leak goroutines in Done example. Damien Neil (1): context: fix removal of cancelled timer contexts from parent David Symonds (2): context: Fix WithValue example code. net: add import comments. Sameer Ajmani (1): context: fix TestAllocs to account for ints in interfaces
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
package ctxutil
|
|
|
|
import (
|
|
"io"
|
|
|
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
)
|
|
|
|
type ioret struct {
|
|
n int
|
|
err error
|
|
}
|
|
|
|
type Writer interface {
|
|
io.Writer
|
|
}
|
|
|
|
type ctxWriter struct {
|
|
w io.Writer
|
|
ctx context.Context
|
|
}
|
|
|
|
// NewWriter wraps a writer to make it respect given Context.
|
|
// If there is a blocking write, the returned Writer will return
|
|
// whenever the context is cancelled (the return values are n=0
|
|
// and err=ctx.Err().)
|
|
//
|
|
// Note well: this wrapper DOES NOT ACTUALLY cancel the underlying
|
|
// write-- there is no way to do that with the standard go io
|
|
// interface. So the read and write _will_ happen or hang. So, use
|
|
// this sparingly, make sure to cancel the read or write as necesary
|
|
// (e.g. closing a connection whose context is up, etc.)
|
|
//
|
|
// Furthermore, in order to protect your memory from being read
|
|
// _after_ you've cancelled the context, this io.Writer will
|
|
// first make a **copy** of the buffer.
|
|
func NewWriter(ctx context.Context, w io.Writer) *ctxWriter {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return &ctxWriter{ctx: ctx, w: w}
|
|
}
|
|
|
|
func (w *ctxWriter) Write(buf []byte) (int, error) {
|
|
buf2 := make([]byte, len(buf))
|
|
copy(buf2, buf)
|
|
|
|
c := make(chan ioret, 1)
|
|
|
|
go func() {
|
|
n, err := w.w.Write(buf2)
|
|
c <- ioret{n, err}
|
|
close(c)
|
|
}()
|
|
|
|
select {
|
|
case r := <-c:
|
|
return r.n, r.err
|
|
case <-w.ctx.Done():
|
|
return 0, w.ctx.Err()
|
|
}
|
|
}
|
|
|
|
type Reader interface {
|
|
io.Reader
|
|
}
|
|
|
|
type ctxReader struct {
|
|
r io.Reader
|
|
ctx context.Context
|
|
}
|
|
|
|
// NewReader wraps a reader to make it respect given Context.
|
|
// If there is a blocking read, the returned Reader will return
|
|
// whenever the context is cancelled (the return values are n=0
|
|
// and err=ctx.Err().)
|
|
//
|
|
// Note well: this wrapper DOES NOT ACTUALLY cancel the underlying
|
|
// write-- there is no way to do that with the standard go io
|
|
// interface. So the read and write _will_ happen or hang. So, use
|
|
// this sparingly, make sure to cancel the read or write as necesary
|
|
// (e.g. closing a connection whose context is up, etc.)
|
|
//
|
|
// Furthermore, in order to protect your memory from being read
|
|
// _before_ you've cancelled the context, this io.Reader will
|
|
// allocate a buffer of the same size, and **copy** into the client's
|
|
// if the read succeeds in time.
|
|
func NewReader(ctx context.Context, r io.Reader) *ctxReader {
|
|
return &ctxReader{ctx: ctx, r: r}
|
|
}
|
|
|
|
func (r *ctxReader) Read(buf []byte) (int, error) {
|
|
buf2 := make([]byte, len(buf))
|
|
|
|
c := make(chan ioret, 1)
|
|
|
|
go func() {
|
|
n, err := r.r.Read(buf2)
|
|
c <- ioret{n, err}
|
|
close(c)
|
|
}()
|
|
|
|
select {
|
|
case ret := <-c:
|
|
copy(buf, buf2)
|
|
return ret.n, ret.err
|
|
case <-r.ctx.Done():
|
|
return 0, r.ctx.Err()
|
|
}
|
|
}
|