Skip to content

feat: gateway improvements#45

Merged
x032205 merged 3 commits intomainfrom
ENG-3959
Oct 24, 2025
Merged

feat: gateway improvements#45
x032205 merged 3 commits intomainfrom
ENG-3959

Conversation

@x032205
Copy link
Member

@x032205 x032205 commented Oct 24, 2025

Description 📣

Make heartbeat run after relay connection & improve user-facing message after accessing an account

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Overview

Greptile Summary

This PR makes two improvements to the gateway system:

  • Heartbeat timing optimization: Moved heartbeat initialization from gateway startup to after successful relay connection establishment. This ensures the heartbeat only starts when the gateway can actually communicate with the relay server. The implementation uses a mutex-protected startHeartbeatOnce() method to guarantee single initialization across reconnections.

  • Enhanced user messaging: Improved the database proxy session message with clearer formatting and instructions, making it easier for users to understand how to connect to their database resource.

The changes are well-isolated, maintain backward compatibility, and follow good concurrency practices with proper mutex protection.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are straightforward and well-implemented. The heartbeat refactoring improves reliability by ensuring it only starts after successful connection, and uses proper concurrency control. The message formatting changes are purely cosmetic with no behavioral impact. No security, logic, or syntax issues found.
  • No files require special attention

Important Files Changed

File Analysis

Filename Score Overview
packages/gateway-v2/gateway.go 5/5 Refactored heartbeat initialization to start after relay connection is established, with proper mutex protection to ensure single initialization
packages/pam/local-database-proxy.go 5/5 Enhanced user-facing output with better formatted connection instructions

Sequence Diagram

sequenceDiagram
    participant Start as Gateway.Start()
    participant Connect as connectAndServe()
    participant Retry as connectWithRetry()
    participant Heartbeat as startHeartbeatOnce()
    participant HB as registerHeartBeat()
    participant Relay as Relay Server

    Start->>Start: Create errCh channel
    Start->>Start: Start certificate renewal
    Start->>Start: Start session uploader
    Start->>Connect: Call connectAndServe(ctx, errCh)
    Connect->>Connect: Register gateway
    Connect->>Retry: Call connectWithRetry(ctx, errCh)
    
    loop Retry up to 6 attempts
        Retry->>Relay: Attempt SSH connection
        alt Connection fails
            Relay-->>Retry: Error
            Retry->>Retry: Wait and retry
        else Connection succeeds
            Relay-->>Retry: SSH client established
            Retry->>Heartbeat: Call startHeartbeatOnce(ctx, errCh)
            Heartbeat->>Heartbeat: Lock heartbeatMu
            alt !heartbeatStarted
                Heartbeat->>HB: registerHeartBeat(ctx, errCh)
                Heartbeat->>Heartbeat: Set heartbeatStarted = true
                HB->>HB: Start goroutine
                Note over HB: Phase 1: Retry every 10s until success
                Note over HB: Phase 2: Regular heartbeat every 30min
            end
            Heartbeat->>Heartbeat: Unlock heartbeatMu
            Retry->>Retry: Handle connection
        end
    end
Loading

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@x032205 x032205 merged commit c51b737 into main Oct 24, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants