@@ -3,6 +3,8 @@ package sqlserver
3
3
import (
4
4
"context"
5
5
"database/sql"
6
+ "net"
7
+ "strconv"
6
8
"time"
7
9
8
10
regexp "github.com/wasilibs/go-re2"
@@ -52,7 +54,7 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
52
54
}
53
55
54
56
if verify {
55
- isVerified , err := ping (paramsUnsafe )
57
+ isVerified , err := ping (ctx , paramsUnsafe )
56
58
57
59
s1 .Verified = isVerified
58
60
@@ -71,7 +73,20 @@ func (s Scanner) FromData(ctx context.Context, verify bool, data []byte) (result
71
73
return results , nil
72
74
}
73
75
74
- var ping = func (config msdsn.Config ) (bool , error ) {
76
+ var ping = func (ctx context.Context , config msdsn.Config ) (bool , error ) {
77
+ // TCP connectivity check to prevent indefinite hangs
78
+ address := net .JoinHostPort (config .Host , strconv .Itoa (int (config .Port )))
79
+
80
+ dialer := & net.Dialer {
81
+ Timeout : 3 * time .Second ,
82
+ }
83
+
84
+ tcpConn , err := dialer .DialContext (ctx , "tcp" , address ) // respects context timeout
85
+ if err != nil {
86
+ return false , err
87
+ }
88
+ defer tcpConn .Close ()
89
+
75
90
cleanConfig := msdsn.Config {}
76
91
cleanConfig .Host = config .Host
77
92
cleanConfig .Port = config .Port
@@ -97,7 +112,7 @@ var ping = func(config msdsn.Config) (bool, error) {
97
112
_ = conn .Close ()
98
113
}()
99
114
100
- err = conn .Ping ()
115
+ err = conn .PingContext ( ctx ) // this doesn't seem to respect the context timeout
101
116
if err != nil {
102
117
return false , err
103
118
}
0 commit comments