Skip to content

Commit

Permalink
db(dm): use net.JoinHostPort to generate host-port part of URI (#6248
Browse files Browse the repository at this point in the history
…) (#6341)

close #6249
  • Loading branch information
ti-chi-bot authored Jul 27, 2022
1 parent 9679557 commit e6ae88f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 44 deletions.
11 changes: 6 additions & 5 deletions dm/dm/portal/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path"
Expand All @@ -32,13 +33,12 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/tidb-tools/pkg/dbutil"
"github.com/pingcap/tidb-tools/pkg/filter"
"github.com/unrolled/render"
"go.uber.org/zap"
"gopkg.in/yaml.v2"

"github.com/pingcap/tiflow/dm/dm/config"
"github.com/pingcap/tiflow/dm/pkg/log"
"github.com/pingcap/tiflow/dm/pkg/utils"
"github.com/unrolled/render"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
)

const (
Expand Down Expand Up @@ -589,7 +589,8 @@ func generateTaskFileName(taskName string) string {

// openDB opens a mysql connection FD.
func openDB(cfg DBConfig, timeout int) (*sql.DB, error) {
dbDSN := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4&timeout=%ds", cfg.User, cfg.Password, cfg.Host, cfg.Port, timeout)
hostPort := net.JoinHostPort(cfg.Host, strconv.Itoa(cfg.Port))
dbDSN := fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4&timeout=%ds", cfg.User, cfg.Password, hostPort, timeout)

dbConn, err := sql.Open("mysql", dbDSN)
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions dm/pkg/conn/basedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"database/sql"
"errors"
"fmt"
"net"
"net/url"
"strconv"
"sync"
Expand Down Expand Up @@ -58,8 +59,9 @@ func init() {
func (d *DefaultDBProviderImpl) Apply(config *config.DBConfig) (*BaseDB, error) {
// maxAllowedPacket=0 can be used to automatically fetch the max_allowed_packet variable from server on every connection.
// https://github.com/go-sql-driver/mysql#maxallowedpacket
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/?charset=utf8mb4&interpolateParams=true&maxAllowedPacket=0",
config.User, config.Password, config.Host, config.Port)
hostPort := net.JoinHostPort(config.Host, strconv.Itoa(config.Port))
dsn := fmt.Sprintf("%s:%s@tcp(%s)/?charset=utf8mb4&interpolateParams=true&maxAllowedPacket=0",
config.User, config.Password, hostPort)

doFuncInClose := func() {}
if config.Security != nil {
Expand Down
1 change: 1 addition & 0 deletions dm/pkg/conn/basedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestGetBaseConn(t *testing.T) {
ids = append(ids, id)
}
require.Equal(t, []int{1}, ids)
require.NoError(t, rows.Err())

mock.ExpectBegin()
mock.ExpectExec("create database test").WillReturnResult(sqlmock.NewResult(1, 1))
Expand Down
21 changes: 10 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ require (
github.com/frankban/quicktest v1.11.1 // indirect
github.com/getkin/kin-openapi v0.80.0
github.com/gin-gonic/gin v1.7.4
github.com/go-mysql-org/go-mysql v1.4.1-0.20211217061939-06f932768788
github.com/go-mysql-org/go-mysql v1.6.1-0.20220718092400-c855c26b37bd
github.com/go-sql-driver/mysql v1.6.0
github.com/gogo/gateway v1.1.0
github.com/gogo/protobuf v1.3.2
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/google/btree v1.0.0
github.com/google/btree v1.0.1
github.com/google/go-cmp v0.5.6
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0
github.com/google/uuid v1.3.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
Expand All @@ -55,12 +55,12 @@ require (
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c
github.com/pingcap/failpoint v0.0.0-20210316064728-7acb0f0a3dfd
github.com/pingcap/kvproto v0.0.0-20211207042851-78a55fb8e69c
github.com/pingcap/kvproto v0.0.0-20220428033740-e4924274acd8
github.com/pingcap/log v0.0.0-20211207084639-71a2e5860834
github.com/pingcap/tidb v1.1.0-beta.0.20220112120142-af3ed8246cbb
github.com/pingcap/tidb v1.1.0-beta.0.20220726050710-0eeb0bbba660
github.com/pingcap/tidb-tools v5.2.3-0.20211105044302-2dabb6641a6e+incompatible
github.com/pingcap/tidb/parser v0.0.0-20220112120142-af3ed8246cbb
github.com/prometheus/client_golang v1.7.1
github.com/pingcap/tidb/parser v0.0.0-20220726050710-0eeb0bbba660
github.com/prometheus/client_golang v1.11.1
github.com/prometheus/client_model v0.2.0
github.com/r3labs/diff v1.1.0
github.com/rakyll/statik v0.1.7
Expand All @@ -76,7 +76,7 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20210305035536-64b5b1c73954
github.com/tidwall/gjson v1.9.1
github.com/tidwall/sjson v1.2.2
github.com/tikv/client-go/v2 v2.0.0-rc.0.20211229051614-62d6b4a2e8f7
github.com/tikv/client-go/v2 v2.0.0-rc.0.20220614073512-c9d27cd5a9a3
github.com/tikv/pd v1.1.0-beta.0.20211118054146-02848d2660ee
github.com/tinylib/msgp v1.1.0
github.com/uber-go/atomic v1.4.0
Expand All @@ -88,7 +88,7 @@ require (
go.uber.org/goleak v1.1.12
go.uber.org/multierr v1.7.0
go.uber.org/zap v1.19.1
golang.org/x/net v0.0.0-20211020060615-d418f374d309
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
golang.org/x/text v0.3.7
Expand All @@ -97,7 +97,6 @@ require (
google.golang.org/grpc v1.40.0
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/yaml.v2 v2.4.0
sigs.k8s.io/yaml v1.2.0 // indirect
upper.io/db.v3 v3.7.1+incompatible
)

Expand Down
Loading

0 comments on commit e6ae88f

Please sign in to comment.