Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConnectWithDialer #643

Merged
merged 3 commits into from
Nov 19, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions client/conn.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"context"
"crypto/tls"
"fmt"
"net"
Expand Down Expand Up @@ -55,10 +56,23 @@ func getNetProto(addr string) string {
func Connect(addr string, user string, password string, dbName string, options ...func(*Conn)) (*Conn, error) {
proto := getNetProto(addr)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use 10*time.Second, please :)

But, LGTM.

defer cancel()

dialer := &net.Dialer{}

return ConnectWithDialer(ctx, proto, addr, user, password, dbName, dialer.DialContext, options...)
}

// Dialer connects to the address on the named network using the provided context.
type Dialer func(ctx context.Context, network, address string) (net.Conn, error)

// Connect to a MySQL server using the given Dialer.
func ConnectWithDialer(ctx context.Context, network string, addr string, user string, password string, dbName string, dialer Dialer, options ...func(*Conn)) (*Conn, error) {
c := new(Conn)

var err error
conn, err := net.DialTimeout(proto, addr, 10*time.Second)
conn, err := dialer(ctx, network, addr)
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -72,9 +86,9 @@ func Connect(addr string, user string, password string, dbName string, options .
c.user = user
c.password = password
c.db = dbName
c.proto = proto
c.proto = network

//use default charset here, utf-8
// use default charset here, utf-8
c.charset = DEFAULT_CHARSET

// Apply configuration functions.
Expand Down