Skip to main content
Lifecycle events allow you to hook into the proxy’s startup and shutdown phases, enabling proper initialization and cleanup of your plugin resources.

Proxy Lifecycle

Gate’s proxy goes through several distinct phases during its lifetime:
  1. Initialization: Proxy is created with configuration
  2. Ready: Proxy starts listening for connections
  3. Running: Proxy handles player connections and server communication
  4. Pre-Shutdown: Proxy stops accepting new connections
  5. Shutdown: Proxy disconnects all players and cleans up resources

ReadyEvent

The ReadyEvent is fired once the proxy has successfully initialized and is ready to serve connections. This is the ideal place to perform post-initialization setup.

Event Structure

When It’s Fired

  • After the proxy successfully binds to its listening address
  • Before any player connections are accepted
  • May be triggered multiple times on config reloads

Example Usage

Use Cases

  • Database Initialization: Connect to databases or external services
  • Dynamic Server Registration: Register backend servers programmatically
  • Background Tasks: Start periodic tasks or workers
  • Plugin Communication: Notify other systems that the proxy is online
  • Metrics Collection: Initialize monitoring and metrics systems

PreShutdownEvent

The PreShutdownEvent is fired after the proxy has stopped accepting new connections but before any players are disconnected. This is your last opportunity to interact with connected players.

Event Structure

When It’s Fired

  • After the proxy stops accepting new connections
  • Before any players are disconnected
  • The proxy will wait for all event listeners to complete

Example Usage

Use Cases

  • Player Transfer: Transfer players to another proxy instance
  • Data Persistence: Save player data before disconnection
  • Custom Disconnect Messages: Provide informative shutdown reasons
  • Graceful Degradation: Notify external services of the shutdown
  • Cleanup Preparation: Prepare for final cleanup operations

ShutdownEvent

The ShutdownEvent is fired after the proxy has stopped accepting connections and after PreShutdownEvent, but before the proxy process exits.

Event Structure

When It’s Fired

  • After all players have been disconnected
  • Before the proxy process terminates
  • After PreShutdownEvent has been processed

Example Usage

Use Cases

  • Resource Cleanup: Close connections, file handles, and other resources
  • Background Task Termination: Stop goroutines and worker pools
  • Final Data Persistence: Save any remaining state to disk
  • External Service Notification: Notify monitoring systems of shutdown
  • Graceful Shutdown: Ensure proper cleanup of plugin dependencies

Complete Lifecycle Example

Here’s a comprehensive example showing how to use all lifecycle events together:

Best Practices

1. Handle Initialization Failures

2. Use Timeouts for Cleanup

3. Handle Multiple Ready Events

Since ReadyEvent may fire multiple times during config reloads, ensure your initialization is idempotent:

4. Log Lifecycle Events

Always log lifecycle events for debugging and monitoring:

See Also