ソースを参照

Move context to first parameter in DialContext

Follows best practice and pkg/context documentation:

> The Context should be the first parameter, typically named ctx
Mathias Fredriksson 7 年 前
コミット
66b9c49e59
2 ファイル変更5 行追加5 行削除
  1. 2 2
      client.go
  2. 3 3
      client_server_test.go

+ 2 - 2
client.go

@@ -103,7 +103,7 @@ type Dialer struct {
 
 // Dial creates a new client connection by calling DialContext with a background context.
 func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
-	return d.DialContext(urlStr, requestHeader, context.Background())
+	return d.DialContext(context.Background(), urlStr, requestHeader)
 }
 
 var errMalformedURL = errors.New("malformed ws or wss URL")
@@ -146,7 +146,7 @@ var nilDialer = *DefaultDialer
 // non-nil *http.Response so that callers can handle redirects, authentication,
 // etcetera. The response body may not contain the entire response and does not
 // need to be closed by the application.
-func (d *Dialer) DialContext(urlStr string, requestHeader http.Header, ctx context.Context) (*Conn, *http.Response, error) {
+func (d *Dialer) DialContext(ctx context.Context, urlStr string, requestHeader http.Header) (*Conn, *http.Response, error) {
 	if d == nil {
 		d = &nilDialer
 	}

+ 3 - 3
client_server_test.go

@@ -424,7 +424,7 @@ func TestHandshakeTimeoutInContext(t *testing.T) {
 
 	ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
 	defer cancel()
-	ws, _, err := d.DialContext(s.URL, nil, ctx)
+	ws, _, err := d.DialContext(ctx, s.URL, nil)
 	if err != nil {
 		t.Fatal("Dial:", err)
 	}
@@ -730,7 +730,7 @@ func TestTracingDialWithContext(t *testing.T) {
 	d := cstDialer
 	d.TLSClientConfig = &tls.Config{RootCAs: certs}
 
-	ws, _, err := d.DialContext(s.URL, nil, ctx)
+	ws, _, err := d.DialContext(ctx, s.URL, nil)
 	if err != nil {
 		t.Fatalf("Dial: %v", err)
 	}
@@ -780,7 +780,7 @@ func TestEmptyTracingDialWithContext(t *testing.T) {
 	d := cstDialer
 	d.TLSClientConfig = &tls.Config{RootCAs: certs}
 
-	ws, _, err := d.DialContext(s.URL, nil, ctx)
+	ws, _, err := d.DialContext(ctx, s.URL, nil)
 	if err != nil {
 		t.Fatalf("Dial: %v", err)
 	}