all: import "context" instead of "golang.org/x/net/context"

There is no need to depend on the old context package now that the
minimum Go version is 1.7. The move to "context" eliminates our weird
vendoring setup. Some vendored code still uses golang.org/x/net/context
and it is now vendored in the normal way.

This change triggered new vet checks around context.WithTimeout which
didn't fire with golang.org/x/net/context.
This commit is contained in:
Felix Lange
2017-03-22 18:20:33 +01:00
parent 525116dbff
commit c213fd1fd8
69 changed files with 187 additions and 437 deletions

View File

@ -19,6 +19,7 @@ package rpc
import (
"bytes"
"container/list"
"context"
"encoding/json"
"errors"
"fmt"
@ -31,7 +32,6 @@ import (
"time"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/net/context"
)
var (

View File

@ -1,60 +0,0 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build !go1.5
package rpc
import (
"net"
"net/http"
"time"
"golang.org/x/net/context"
)
// In older versions of Go (below 1.5), dials cannot be canceled
// via a channel or context. The context deadline can still applied.
// contextDialer returns a dialer that applies the deadline value from the given context.
func contextDialer(ctx context.Context) *net.Dialer {
dialer := &net.Dialer{KeepAlive: tcpKeepAliveInterval}
if deadline, ok := ctx.Deadline(); ok {
dialer.Deadline = deadline
} else {
dialer.Deadline = time.Now().Add(defaultDialTimeout)
}
return dialer
}
// dialContext connects to the given address, aborting the dial if ctx is canceled.
func dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return contextDialer(ctx).Dial(network, addr)
}
// requestWithContext copies req, adding the cancelation channel and deadline from ctx.
func requestWithContext(c *http.Client, req *http.Request, ctx context.Context) (*http.Client, *http.Request) {
// Set Timeout on the client if the context has a deadline.
// Note that there is no default timeout (unlike in contextDialer) because
// the timeout applies to the entire request, including reads from body.
if deadline, ok := ctx.Deadline(); ok {
c2 := *c
c2.Timeout = deadline.Sub(time.Now())
c = &c2
}
req2 := *req
return c, &req2
}

View File

@ -1,61 +0,0 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build go1.5,!go1.6
package rpc
import (
"net"
"net/http"
"time"
"golang.org/x/net/context"
)
// In Go 1.5, dials cannot be canceled via a channel or context. The context deadline can
// still be applied. Go 1.5 adds the ability to cancel HTTP requests via a channel.
// contextDialer returns a dialer that applies the deadline value from the given context.
func contextDialer(ctx context.Context) *net.Dialer {
dialer := &net.Dialer{KeepAlive: tcpKeepAliveInterval}
if deadline, ok := ctx.Deadline(); ok {
dialer.Deadline = deadline
} else {
dialer.Deadline = time.Now().Add(defaultDialTimeout)
}
return dialer
}
// dialContext connects to the given address, aborting the dial if ctx is canceled.
func dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return contextDialer(ctx).Dial(network, addr)
}
// requestWithContext copies req, adding the cancelation channel and deadline from ctx.
func requestWithContext(c *http.Client, req *http.Request, ctx context.Context) (*http.Client, *http.Request) {
// Set Timeout on the client if the context has a deadline.
// Note that there is no default timeout (unlike in contextDialer) because
// the timeout applies to the entire request, including reads from body.
if deadline, ok := ctx.Deadline(); ok {
c2 := *c
c2.Timeout = deadline.Sub(time.Now())
c = &c2
}
req2 := *req
req2.Cancel = ctx.Done()
return c, &req2
}

View File

@ -1,55 +0,0 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build go1.6,!go1.7
package rpc
import (
"net"
"net/http"
"time"
"golang.org/x/net/context"
)
// In Go 1.6, net.Dialer gained the ability to cancel via a channel.
// contextDialer returns a dialer that applies the deadline value from the given context.
func contextDialer(ctx context.Context) *net.Dialer {
dialer := &net.Dialer{Cancel: ctx.Done(), KeepAlive: tcpKeepAliveInterval}
if deadline, ok := ctx.Deadline(); ok {
dialer.Deadline = deadline
} else {
dialer.Deadline = time.Now().Add(defaultDialTimeout)
}
return dialer
}
// dialContext connects to the given address, aborting the dial if ctx is canceled.
func dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
return contextDialer(ctx).Dial(network, addr)
}
// requestWithContext copies req, adding the cancelation channel and deadline from ctx.
func requestWithContext(c *http.Client, req *http.Request, ctx context.Context) (*http.Client, *http.Request) {
// We set Timeout on the client for Go <= 1.5. There
// is no need to do that here because the dial will be canceled
// by package http.
req2 := *req
req2.Cancel = ctx.Done()
return c, &req2
}

View File

@ -1,51 +0,0 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
// +build go1.7
package rpc
import (
"context"
"net"
"net/http"
"time"
)
// In Go 1.7, context moved into the standard library and support
// for cancelation via context was added to net.Dialer and http.Request.
// contextDialer returns a dialer that applies the deadline value from the given context.
func contextDialer(ctx context.Context) *net.Dialer {
dialer := &net.Dialer{Cancel: ctx.Done(), KeepAlive: tcpKeepAliveInterval}
if deadline, ok := ctx.Deadline(); ok {
dialer.Deadline = deadline
} else {
dialer.Deadline = time.Now().Add(defaultDialTimeout)
}
return dialer
}
// dialContext connects to the given address, aborting the dial if ctx is canceled.
func dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{KeepAlive: tcpKeepAliveInterval}
return d.DialContext(ctx, network, addr)
}
// requestWithContext copies req, adding the cancelation channel and deadline from ctx.
func requestWithContext(c *http.Client, req *http.Request, ctx context.Context) (*http.Client, *http.Request) {
return c, req.WithContext(ctx)
}

View File

@ -17,12 +17,12 @@
package rpc_test
import (
"context"
"fmt"
"math/big"
"time"
"github.com/ethereum/go-ethereum/rpc"
"golang.org/x/net/context"
)
// In this example, our client whishes to track the latest 'block number'

View File

@ -17,6 +17,7 @@
package rpc
import (
"context"
"fmt"
"math/rand"
"net"
@ -31,7 +32,6 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/net/context"
)
func TestClientRequest(t *testing.T) {

View File

@ -18,6 +18,7 @@ package rpc
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@ -29,7 +30,6 @@ import (
"time"
"github.com/rs/cors"
"golang.org/x/net/context"
)
const (
@ -115,11 +115,11 @@ func (hc *httpConn) doRequest(ctx context.Context, msg interface{}) (io.ReadClos
if err != nil {
return nil, err
}
client, req := requestWithContext(hc.client, hc.req, ctx)
req := hc.req.WithContext(ctx)
req.Body = ioutil.NopCloser(bytes.NewReader(body))
req.ContentLength = int64(len(body))
resp, err := client.Do(req)
resp, err := hc.client.Do(req)
if err != nil {
return nil, err
}

View File

@ -17,9 +17,8 @@
package rpc
import (
"context"
"net"
"golang.org/x/net/context"
)
// NewInProcClient attaches an in-process connection to the given RPC server.

View File

@ -17,12 +17,11 @@
package rpc
import (
"context"
"fmt"
"net"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/net/context"
)
// CreateIPCListener creates an listener, on Unix platforms this is a unix socket, on

View File

@ -19,11 +19,10 @@
package rpc
import (
"context"
"net"
"os"
"path/filepath"
"golang.org/x/net/context"
)
// ipcListen will create a Unix socket on the given endpoint.

View File

@ -19,10 +19,10 @@
package rpc
import (
"context"
"net"
"time"
"golang.org/x/net/context"
"gopkg.in/natefinch/npipe.v2"
)

View File

@ -17,14 +17,13 @@
package rpc
import (
"context"
"fmt"
"reflect"
"runtime"
"sync/atomic"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/net/context"
"gopkg.in/fatih/set.v0"
)

View File

@ -17,13 +17,12 @@
package rpc
import (
"context"
"encoding/json"
"net"
"reflect"
"testing"
"time"
"golang.org/x/net/context"
)
type Service struct{}

View File

@ -17,10 +17,9 @@
package rpc
import (
"context"
"errors"
"sync"
"golang.org/x/net/context"
)
var (

View File

@ -17,13 +17,12 @@
package rpc
import (
"context"
"encoding/json"
"net"
"sync"
"testing"
"time"
"golang.org/x/net/context"
)
type NotificationTestService struct {

View File

@ -18,6 +18,7 @@ package rpc
import (
"bufio"
"context"
crand "crypto/rand"
"encoding/binary"
"encoding/hex"
@ -29,8 +30,6 @@ import (
"time"
"unicode"
"unicode/utf8"
"golang.org/x/net/context"
)
var (

View File

@ -17,6 +17,7 @@
package rpc
import (
"context"
"crypto/tls"
"fmt"
"net"
@ -24,10 +25,9 @@ import (
"net/url"
"os"
"strings"
"time"
"github.com/ethereum/go-ethereum/log"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
"gopkg.in/fatih/set.v0"
)
@ -150,3 +150,18 @@ func wsDialAddress(location *url.URL) string {
}
return location.Host
}
func dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
d := &net.Dialer{KeepAlive: tcpKeepAliveInterval}
return d.DialContext(ctx, network, addr)
}
func contextDialer(ctx context.Context) *net.Dialer {
dialer := &net.Dialer{Cancel: ctx.Done(), KeepAlive: tcpKeepAliveInterval}
if deadline, ok := ctx.Deadline(); ok {
dialer.Deadline = deadline
} else {
dialer.Deadline = time.Now().Add(defaultDialTimeout)
}
return dialer
}