mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Introduce a new method mysqlConn.Reset()

Open zenovich opened this issue 4 months ago โ€ข 8 comments

Description

This PR introduces a new method mysqlConn.Reset() sending COM_RESET_CONNECTION to the connection and checking the result.

As discussed in #1273, although there are strong reasons (https://github.com/go-sql-driver/mysql/issues/1273#issuecomment-2227210191, https://github.com/go-sql-driver/mysql/issues/1273#issuecomment-3202690653) to call this new method in mysqlConn.ResetSession() (probably at the end of the method), we are not going to call it for now because it has not been decided yet.

Here, we only add the implementation to allow people not to go through creating dirty hacks like those here: https://github.com/France-ioi/AlgoreaBackend/blob/master/app/database/mysql_conn_reset.go#L95

Checklist

  • [x] Code compiles correctly
  • [x] Created tests which fail without the change (if possible)
  • [x] All tests passing
  • [x] Extended the README / documentation, if necessary
  • [x] Added myself / the copyright holder to the AUTHORS file

zenovich avatar Aug 25 '25 23:08 zenovich

Walkthrough

Adds three MySQL command opcodes, a reusable no-argument command helper, a public mysqlConn.Reset(ctx) method, refactors Ping to use the helper, consolidates connection tests into table-driven cases for Ping/Reset, expands driver tests for Reset behavior, and appends an AUTHORS entry.

Changes

Cohort / File(s) Summary
Authorship metadata
AUTHORS
Adds "Dmitry Zenovich " under Individual Persons.
Command opcodes
const.go
Appends comDaemon, comBinlogDumpGTID, and comResetConnection to the MySQL command opcode list (new byte constants).
No-arg command core & public API
connection.go
Adds sendSimpleCommandOK(ctx context.Context, cmd byte) helper; adds func (mc *mysqlConn) Reset(ctx context.Context) error which sends COM_RESET_CONNECTION; refactors Ping to delegate to the new helper.
Connection unit tests
connection_test.go
Replaces Ping-specific tests with table-driven TestSimpleCommandOK* tests exercising Ping and Reset for clean cancelation, bad-connection marking, and invalid underlying connection scenarios.
Driver/integration tests
driver_test.go
Adds TestSimpleCommandOK and TestReset to exercise Ping/Reset via database/sql.Conn.Raw, verify internal buffers are cleared and session variable reset; minor cleanup to Ping test setup.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant MC as mysqlConn
  participant Net as net.Conn
  participant S as MySQL Server

  rect rgb(245,250,255)
    note over Client,MC: No-arg command flow (Ping / Reset)
    Client->>MC: Ping(ctx) / Reset(ctx)
    MC->>MC: sendSimpleCommandOK(ctx, cmd)
    MC->>Net: writeCommandPacket(cmd)
    Net->>S: COM_PING / COM_RESET_CONNECTION
    S-->>Net: OK packet
    Net-->>MC: OK packet
    MC->>MC: readResultOK()
    MC-->>Client: return (nil or error)
  end
sequenceDiagram
  autonumber
  actor Test
  participant DB as database/sql.Conn
  participant MC as mysqlConn
  participant S as MySQL Server

  note over Test,MC: Reset clears server session state
  Test->>S: SET @a := 1
  Test->>S: SELECT @a  (expect 1)
  Test->>DB: Conn.Raw => mc
  Test->>MC: Reset(ctx)
  MC->>S: COM_RESET_CONNECTION
  S-->>MC: OK
  Test->>S: SELECT @a  (expect NULL)

Estimated code review effort

๐ŸŽฏ 3 (Moderate) | โฑ๏ธ ~30 minutes

[!TIP]

๐Ÿ”Œ Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

โœจ Finishing Touches
  • [ ] ๐Ÿ“ Generate Docstrings
๐Ÿงช Generate unit tests
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

โค๏ธ Share
๐Ÿชง Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Aug 25 '25 23:08 coderabbitai[bot]

Probably we could add the new method into the interface introduced in #1454 to allow calling it from outside easier.

zenovich avatar Aug 26 '25 02:08 zenovich

PTAL

zenovich avatar Aug 26 '25 12:08 zenovich

PTAL

zenovich avatar Aug 26 '25 13:08 zenovich

PTAL

zenovich avatar Aug 26 '25 13:08 zenovich

PTAL

zenovich avatar Aug 26 '25 13:08 zenovich

Actionable comments posted: 0

๐Ÿงน Nitpick comments (2)

driver_test.go (2)> 2355-2368: Avoid potential flakiness: verify result after rows.Close, not before

For SELECT, the driver may finalize result metadata (affectedRows/insertIds) only after the result set is fully consumed/closed. Checking these fields before q.Close risks a race across server versions/protocols. Move the assertions below q.Close:

-					if got, want := c.result.affectedRows, []int64{0}; !reflect.DeepEqual(got, want) {
-						dbt.Fatalf("bad affectedRows: got %v, want=%v", got, want)
-					}
-					if got, want := c.result.insertIds, []int64{0}; !reflect.DeepEqual(got, want) {
-						dbt.Fatalf("bad insertIds: got %v, want=%v", got, want)
-					}
 					if err := q.Close(); err != nil {
 						dbt.fail("Rows", "Close", err)
 					}
+					if got := c.result.affectedRows; len(got) != 1 || got[0] != 0 {
+						dbt.Fatalf("bad affectedRows: got %v, want [0]", got)
+					}
+					if got := c.result.insertIds; len(got) != 1 || got[0] != 0 {
+						dbt.Fatalf("bad insertIds: got %v, want [0]", got)
+					}

2380-2385: Prefer explicit nil checks over reflect.DeepEqual for slices These assertions are specifically verifying nil (not just โ€œemptyโ€). Use direct nil checks for clarity and to avoid reflect costs.

-						if got, want := c.result.affectedRows, []int64(nil); !reflect.DeepEqual(got, want) {
-							t.Errorf("bad affectedRows: got %v, want=%v", got, want)
-						}
-						if got, want := c.result.insertIds, []int64(nil); !reflect.DeepEqual(got, want) {
-							t.Errorf("bad insertIds: got %v, want=%v", got, want)
-						}
+						if c.result.affectedRows != nil {
+							t.Errorf("bad affectedRows: got %v, want nil", c.result.affectedRows)
+						}
+						if c.result.insertIds != nil {
+							t.Errorf("bad insertIds: got %v, want nil", c.result.insertIds)
+						}

๐Ÿ“œ Review details Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

๐Ÿ’ก Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

๐Ÿ“ฅ Commits ๐Ÿ“’ Files selected for processing (1) ๐Ÿงฐ Additional context used ๐Ÿ”‡ Additional comments (2)

This is something that had been done this way before the PR was created.

zenovich avatar Aug 26 '25 13:08 zenovich

LGTM

zenovich avatar Sep 05 '25 20:09 zenovich