Repository: vitessio/vitess (Go)

Issue:
Switch flag definitions to be on pflag instead of flag in `package go/mysql`
Part of https://github.com/vitessio/vitess/issues/10697.

Current flags:

```
$ git grep -E "\bflag\.[A-Z]" -- go/mysql/*.go
go/mysql/auth_server_clientcert.go:var clientcertAuthMethod = flag.String("mysql_clientcert_auth_method", string(MysqlClearPassword), "client-side authentication method to use. Supported values: mysql_clear_password, dialog.")
go/mysql/auth_server_clientcert.go:     if flag.CommandLine.Lookup("mysql_server_ssl_ca").Value.String() == "" {
go/mysql/auth_server_static.go: mysqlAuthServerStaticFile           = flag.String("mysql_auth_server_static_file", "", "JSON File to read the users/passwords from.")
go/mysql/auth_server_static.go: mysqlAuthServerStaticString         = flag.String("mysql_auth_server_static_string", "", "JSON representation of the users/passwords config.")
go/mysql/auth_server_static.go: mysqlAuthServerStaticReloadInterval = flag.Duration("mysql_auth_static_reload_interval", 0, "Ticker to reload credentials")
go/mysql/conn.go:var mysqlServerFlushDelay = flag.Duration("mysql_server_flush_delay", 100*time.Millisecond, "Delay after which buffered response will be flushed to the client.")
go/mysql/query_benchmark_test.go:       flag.IntVar(&testReadConnBufferSize, "test.read_conn_buffer_size", connBufferSize, "buffer size for reads from connections in tests")
```


Discussion:

Patch:
diff --git a/go/flags/endtoend/vtctld.txt b/go/flags/endtoend/vtctld.txt
index 60367b9dbc7..887a7496b48 100644
--- a/go/flags/endtoend/vtctld.txt
+++ b/go/flags/endtoend/vtctld.txt
@@ -87,7 +87,6 @@ Usage of vtctld:
       --log_rotate_max_size uint                                         size in bytes at which logs are rotated (glog.MaxSize) (default 1887436800)
       --logtostderr                                                      log to standard error instead of files
       --master_connect_retry duration                                    Deprecated, use -replication_connect_retry (default 10s)
-      --mysql_server_flush_delay duration                                Delay after which buffered response will be flushed to the client. (default 100ms)
       --mysql_server_version string                                      MySQL server version to advertise.
       --mysqlctl_client_protocol string                                  the protocol to use to talk to the mysqlctl server (default "grpc")
       --mysqlctl_mycnf_template string                                   template file to use for generating the my.cnf file during server init
diff --git a/go/flags/endtoend/vtexplain.txt b/go/flags/endtoend/vtexplain.txt
index 032a77ddf6b..807cd0a74a5 100644
--- a/go/flags/endtoend/vtexplain.txt
+++ b/go/flags/endtoend/vtexplain.txt
@@ -68,7 +68,6 @@ Usage of vtexplain:
       --mysql_auth_server_impl string                   Which auth server implementation to use. Options: none, ldap, clientcert, static, vault. (default "static")
       --mysql_default_workload string                   Default session workload (OLTP, OLAP, DBA) (default "OLTP")
       --mysql_server_bind_address string                Binds on this address when listening to MySQL binary protocol. Useful to restrict listening to 'localhost' only for instance.
-      --mysql_server_flush_delay duration               Delay after which buffered response will be flushed to the client. (default 100ms)
       --mysql_server_port int                           If set, also listen for MySQL binary protocol connections on this port. (default -1)
       --mysql_server_query_timeout duration             mysql query timeout (default 0s)
       --mysql_server_read_timeout duration              connection read timeout (default 0s)
diff --git a/go/flags/endtoend/vtgr.txt b/go/flags/endtoend/vtgr.txt
index 8ce42ff2b42..5829f6e0280 100644
--- a/go/flags/endtoend/vtgr.txt
+++ b/go/flags/endtoend/vtgr.txt
@@ -27,7 +27,6 @@ Usage of vtgr:
       --log_err_stacks                             log stack traces for errors
       --log_rotate_max_size uint                   size in bytes at which logs are rotated (glog.MaxSize) (default 1887436800)
       --logtostderr                                log to standard error instead of files
-      --mysql_server_flush_delay duration          Delay after which buffered response will be flushed to the client. (default 100ms)
       --ping_tablet_timeout duration               time to wait when we ping a tablet (default 2s)
       --pprof strings                              enable profiling
       --purge_logs_interval duration               how often try to remove old logs (default 1h0m0s)
diff --git a/go/flags/endtoend/vtorc.txt b/go/flags/endtoend/vtorc.txt
index 49354958c2b..c481bfea170 100644
--- a/go/flags/endtoend/vtorc.txt
+++ b/go/flags/endtoend/vtorc.txt
@@ -28,7 +28,6 @@ Usage of vtorc:
       --log_err_stacks                           log stack traces for errors
       --log_rotate_max_size uint                 size in bytes at which logs are rotated (glog.MaxSize) (default 1887436800)
       --logtostderr                              log to standard error instead of files
-      --mysql_server_flush_delay duration        Delay after which buffered response will be flushed to the client. (default 100ms)
       --noop                                     Dry run; do not perform destructing operations
       --onclose_timeout duration                 wait no more than this for OnClose handlers before stopping (default 1ns)
       --onterm_timeout duration                  wait no more than this for OnTermSync handlers before stopping (default 10s)
diff --git a/go/flags/endtoend/vttablet.txt b/go/flags/endtoend/vttablet.txt
index 429c4044070..1b8265601ea 100644
--- a/go/flags/endtoend/vttablet.txt
+++ b/go/flags/endtoend/vttablet.txt
@@ -284,7 +284,6 @@ Usage of vttablet:
       --mycnf_slow_log_path string                                       mysql slow query log path
       --mycnf_socket_file string                                         mysql socket file
       --mycnf_tmp_dir string                                             mysql tmp directory
-      --mysql_server_flush_delay duration                                Delay after which buffered response will be flushed to the client. (default 100ms)
       --mysql_server_version string                                      MySQL server version to advertise.
       --mysqlctl_client_protocol string                                  the protocol to use to talk to the mysqlctl server (default "grpc")
       --mysqlctl_mycnf_template string                                   template file to use for generating the my.cnf file during server init
diff --git a/go/mysql/auth_server_static.go b/go/mysql/auth_server_static.go
index bb0a966d7e7..d01bab23cdd 100644
--- a/go/mysql/auth_server_static.go
+++ b/go/mysql/auth_server_static.go
@@ -41,6 +41,7 @@ var (
 	mysqlAuthServerStaticFile           string
 	mysqlAuthServerStaticString         string
 	mysqlAuthServerStaticReloadInterval time.Duration
+	mysqlServerFlushDelay               = 100 * time.Millisecond
 )
 
 func init() {
@@ -48,6 +49,7 @@ func init() {
 		fs.StringVar(&mysqlAuthServerStaticFile, "mysql_auth_server_static_file", "", "JSON File to read the users/passwords from.")
 		fs.StringVar(&mysqlAuthServerStaticString, "mysql_auth_server_static_string", "", "JSON representation of the users/passwords config.")
 		fs.DurationVar(&mysqlAuthServerStaticReloadInterval, "mysql_auth_static_reload_interval", 0, "Ticker to reload credentials")
+		fs.DurationVar(&mysqlServerFlushDelay, "mysql_server_flush_delay", mysqlServerFlushDelay, "Delay after which buffered response will be flushed to the client.")
 	})
 }
 
diff --git a/go/mysql/conn.go b/go/mysql/conn.go
index f6d87344546..c1da4ef94da 100644
--- a/go/mysql/conn.go
+++ b/go/mysql/conn.go
@@ -21,7 +21,6 @@ import (
 	"crypto/tls"
 	"crypto/x509"
 	"errors"
-	"flag"
 	"fmt"
 	"io"
 	"net"
@@ -43,8 +42,6 @@ import (
 	"vitess.io/vitess/go/vt/vterrors"
 )
 
-var mysqlServerFlushDelay = flag.Duration("mysql_server_flush_delay", 100*time.Millisecond, "Delay after which buffered response will be flushed to the client.")
-
 const (
 	// connBufferSize is how much we buffer for reading and
 	// writing. It is also how much we allocate for ephemeral buffers.
@@ -309,7 +306,7 @@ func (c *Conn) getWriter() (w io.Writer, unget func()) {
 // startFlushTimer must be called while holding lock on bufMu.
 func (c *Conn) startFlushTimer() {
 	c.stopFlushTimer()
-	c.flushTimer = time.AfterFunc(*mysqlServerFlushDelay, func() {
+	c.flushTimer = time.AfterFunc(mysqlServerFlushDelay, func() {
 		c.bufMu.Lock()
 		defer c.bufMu.Unlock()
 


Test patch:
diff --git a/go/mysql/query_benchmark_test.go b/go/mysql/query_benchmark_test.go
index db056398500..f90dfc9e89a 100644
--- a/go/mysql/query_benchmark_test.go
+++ b/go/mysql/query_benchmark_test.go
@@ -17,21 +17,16 @@ limitations under the License.
 package mysql
 
 import (
-	"flag"
+	"context"
 	"math/rand"
 	"net"
 	"strings"
 	"testing"
-
-	"context"
 )
 
+// Override the default here to test with different values.
 var testReadConnBufferSize = connBufferSize
 
-func init() {
-	flag.IntVar(&testReadConnBufferSize, "test.read_conn_buffer_size", connBufferSize, "buffer size for reads from connections in tests")
-}
-
 const benchmarkQueryPrefix = "benchmark "
 
 func benchmarkQuery(b *testing.B, threads int, query string) {
diff --git a/go/mysql/server_flaky_test.go b/go/mysql/server_flaky_test.go
index 6ec95aef594..b89476a2a75 100644
--- a/go/mysql/server_flaky_test.go
+++ b/go/mysql/server_flaky_test.go
@@ -1459,8 +1459,8 @@ func TestParseConnAttrs(t *testing.T) {
 }
 
 func TestServerFlush(t *testing.T) {
-	defer func(saved time.Duration) { *mysqlServerFlushDelay = saved }(*mysqlServerFlushDelay)
-	*mysqlServerFlushDelay = 10 * time.Millisecond
+	defer func(saved time.Duration) { mysqlServerFlushDelay = saved }(mysqlServerFlushDelay)
+	mysqlServerFlushDelay = 10 * time.Millisecond
 
 	th := &testHandler{}
 
@@ -1485,8 +1485,8 @@ func TestServerFlush(t *testing.T) {
 
 	flds, err := c.Fields()
 	require.NoError(t, err)
-	if duration, want := time.Since(start), 20*time.Millisecond; duration < *mysqlServerFlushDelay || duration > want {
-		assert.Fail(t, "duration out of expected range", "duration: %v, want between %v and %v", duration.String(), (*mysqlServerFlushDelay).String(), want.String())
+	if duration, want := time.Since(start), 20*time.Millisecond; duration < mysqlServerFlushDelay || duration > want {
+		assert.Fail(t, "duration out of expected range", "duration: %v, want between %v and %v", duration.String(), (mysqlServerFlushDelay).String(), want.String())
 	}
 	want1 := []*querypb.Field{{
 		Name: "result",