Guide: Hot Reload Implementation #66
switchupcb
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Creating or debugging a Discord Bot in Go can be time-consuming due to the need to
go build
after every change. Interpreted languages (i.e Python, JavaScript) don't require building since the code is interpreted as it's required (using a Just In Time Compiler).Using Go (and Disgo) increases code velocity and performance due to ease of concurrency usage and compilation. If we can figure out a way to Hot Reload during the build process, code iteration that involves the build process can be handled with minimal effort. a
This functionality provides a better developer experience to the end user (developer).
What is Hot Reloading?
Live Reloading reloads or refreshes the entire app when a file changes. Hot Reloading only refreshes the files that were changed without losing the state of the app.
Live Reloading reloads or refreshes the entire app when a file changes. Hot Reloading only refreshes the files that were changed without losing the state of the app.
For more information, read What is the difference between Hot Reloading and Live Reloading?.
How can you Hot Reload in Go?
The first option is to use an interpreter.
For example,
copygen
usesyaegi
to interprettemplate.go
files that generate code. As a result, there is no requirement to continuously rungo build
during the code generation process.Unfortunately, none of the available interpreters for Go support third-party modules (to the standard Go library; i.e Disgo).
yaegi
has yet to review my pull request for Go module support in feat: support for Go modules to imports [needs unit test review] traefik/yaegi#1265 (comment).gomacro
does not support 3rd party imports cross-platform (tracked in Fix 3rd Party Imports Cross-Platform cosmos72/gomacro#121).This approach is also not performant.
The alternative option is to selectively build code as it changes.
This functionality is possible with a Live Reload module such as
air
. However, live reloading implies that the entire application is restarted, which means that existing WebSocket Connections would be lost in the context of a single binary.The above issue means that a Go Discord Bot that creates a function or maintains a state using a single binary would not be possible. Instead, one must create multiple binaries and only hot reload them when a state is not required.
What is a cluster?
A computer cluster describes a group of computers (servers) running together to act as a single system. Modern software clusters involve containers (such as Docker or Podman) and container orchestrators (such as Kubernetes or Nomad) to maintain clusters (of services).
Clusters are typically used in software that involves microservices.
How to Hot Deploy a Discord Bot
A system that supports Hot Reloading for Go Discord Bot's can be architected using the information above,
Disclaimer: Users actually send requests to the REST API to trigger interactions, but this is simplified for the sake of focus.
Design
Go's compilation allows the creation of single-file executables. So a service can be run from a computer using
./service
.This hot reload guide will demonstrate how to implement Hot Reloading for a Discord Bot that uses Application Commands and responds to Interactions (over the WebSocket Connection) using 3 services.
Commands (State)
A
.go
file (compiled to a binary) will be used to maintain the Bot's Application Commands.As requests such as
CreateGlobalApplicationCommands
are idempotent, an ideal Hot Reload implementation uses a stateful initialization of the program to update available commands.WebSocket (Client, Server)
A
.go
file (compiled to a binary) will be used to maintain the Bot's WebSocket Connection to the Discord Gateway.THIS SERVICE MUST NOT BE RELOADED.
Otherwise, the Bot will disconnect from the Discord Gateway and require reconnection. Disgo will handle this reconnection, but since Discord maintains a daily rate limit for the amount of
Identify
send events, it's not recommended to disconnect and reconnect from the Gateway continuously. Therefore, this service must remain unmodified.An additional service must be created to handle interactions, such that interaction handling is hot reloadable.
Response (Client)
A
.go
file (compiled to a binary) will be used to handle the Bot's Interactions using requests (which don't require connections to the Discord Gateway).This implementation is a complement to the WebSocket Service, which can't be reloaded. As a result, this service will be reloaded (frequently) to change how a bot handle's interactions.
The interaction handler must retrieve incoming data from Discord from the WebSocket service, which runs from another application. As a result, the WebSocket server must serve incoming data over the network to the Interaction Handler service, which then sends the Interaction Response to Discord.
This means that the WebSocket Client must also implement a server to serve the Response service data from Discord.
Tasks
Beta Was this translation helpful? Give feedback.
All reactions