Skip to content

Commit c87274a

Browse files
authored
Merge pull request #96 from gatewayd-io/redesign-hook-system
Slight refactoring of the hook system plus adding termination logic
2 parents 9a80d2d + 6c7f64d commit c87274a

20 files changed

+594
-516
lines changed

cmd/run.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/gatewayd-io/gatewayd/logging"
1313
"github.com/gatewayd-io/gatewayd/network"
1414
"github.com/gatewayd-io/gatewayd/plugin"
15+
"github.com/gatewayd-io/gatewayd/plugin/hook"
1516
"github.com/gatewayd-io/gatewayd/pool"
1617
"github.com/knadh/koanf"
1718
"github.com/knadh/koanf/parsers/yaml"
@@ -28,7 +29,7 @@ var (
2829
)
2930

3031
var (
31-
hooksConfig = plugin.NewHookConfig()
32+
hooksConfig = hook.NewHookConfig()
3233
DefaultLogger = logging.NewLogger(
3334
logging.LoggerConfig{
3435
Level: zerolog.DebugLevel,
@@ -108,7 +109,7 @@ var runCmd = &cobra.Command{
108109
updatedGlobalConfig, err := hooksConfig.Run(
109110
context.Background(),
110111
globalConfig.All(),
111-
plugin.OnConfigLoaded,
112+
hook.OnConfigLoaded,
112113
hooksConfig.Verification)
113114
if err != nil {
114115
DefaultLogger.Error().Err(err).Msg("Failed to run OnConfigLoaded hooks")
@@ -154,7 +155,7 @@ var runCmd = &cobra.Command{
154155
}
155156
// TODO: Use a context with a timeout
156157
_, err = hooksConfig.Run(
157-
context.Background(), data, plugin.OnNewLogger, hooksConfig.Verification)
158+
context.Background(), data, hook.OnNewLogger, hooksConfig.Verification)
158159
if err != nil {
159160
logger.Error().Err(err).Msg("Failed to run OnNewLogger hooks")
160161
}
@@ -185,7 +186,7 @@ var runCmd = &cobra.Command{
185186
_, err := hooksConfig.Run(
186187
context.Background(),
187188
clientCfg,
188-
plugin.OnNewClient,
189+
hook.OnNewClient,
189190
hooksConfig.Verification)
190191
if err != nil {
191192
logger.Error().Err(err).Msg("Failed to run OnNewClient hooks")
@@ -213,7 +214,7 @@ var runCmd = &cobra.Command{
213214
_, err = hooksConfig.Run(
214215
context.Background(),
215216
map[string]interface{}{"size": poolSize},
216-
plugin.OnNewPool,
217+
hook.OnNewPool,
217218
hooksConfig.Verification)
218219
if err != nil {
219220
logger.Error().Err(err).Msg("Failed to run OnNewPool hooks")
@@ -240,7 +241,7 @@ var runCmd = &cobra.Command{
240241
},
241242
}
242243
_, err = hooksConfig.Run(
243-
context.Background(), proxyCfg, plugin.OnNewProxy, hooksConfig.Verification)
244+
context.Background(), proxyCfg, hook.OnNewProxy, hooksConfig.Verification)
244245
if err != nil {
245246
logger.Error().Err(err).Msg("Failed to run OnNewProxy hooks")
246247
}
@@ -302,7 +303,7 @@ var runCmd = &cobra.Command{
302303
"tcpNoDelay": gConfig.Server.TCPNoDelay,
303304
}
304305
_, err = hooksConfig.Run(
305-
context.Background(), serverCfg, plugin.OnNewServer, hooksConfig.Verification)
306+
context.Background(), serverCfg, hook.OnNewServer, hooksConfig.Verification)
306307
if err != nil {
307308
logger.Error().Err(err).Msg("Failed to run OnNewServer hooks")
308309
}
@@ -320,15 +321,15 @@ var runCmd = &cobra.Command{
320321
)
321322
signalsCh := make(chan os.Signal, 1)
322323
signal.Notify(signalsCh, signals...)
323-
go func(hooksConfig *plugin.HookConfig) {
324+
go func(hooksConfig *hook.Config) {
324325
for sig := range signalsCh {
325326
for _, s := range signals {
326327
if sig != s {
327328
// Notify the hooks that the server is shutting down.
328329
_, err := hooksConfig.Run(
329330
context.Background(),
330331
map[string]interface{}{"signal": sig.String()},
331-
plugin.OnSignal,
332+
hook.OnSignal,
332333
hooksConfig.Verification,
333334
)
334335
if err != nil {

errors/errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
ErrCodeCastFailed
2424
ErrCodeHookVerificationFailed
2525
ErrCodeHookReturnedError
26+
ErrCodeHookTerminatedConnection
2627
ErrCodeFileNotFound
2728
ErrCodeFileOpenFailed
2829
ErrCodeFileReadFailed
@@ -77,6 +78,8 @@ var (
7778
ErrCodeHookVerificationFailed, "failed to verify hook", nil)
7879
ErrHookReturnedError = NewGatewayDError(
7980
ErrCodeHookReturnedError, "hook returned error", nil)
81+
ErrHookTerminatedConnection = NewGatewayDError(
82+
ErrCodeHookTerminatedConnection, "hook terminated connection", nil)
8083

8184
ErrFileNotFound = NewGatewayDError(
8285
ErrCodeFileNotFound, "file not found", nil)

gatewayd_plugins.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ plugins:
2323
- MAGIC_COOKIE_KEY=GATEWAYD_PLUGIN
2424
- MAGIC_COOKIE_VALUE=5712b87aa5d7e9f9e9ab643e6603181c5b796015cb1c09d6f5ada882bf2a1872
2525
# Checksum hash to verify the binary before loading
26-
checksum: 911cbab556bd3b14b60c088d786ae7c3ecf0a2aa2958406c3214ea64073cde36
26+
checksum: 477dcbb41a27ff4431a5a9bc50754ef8c5a477b62d45d43e3b801b121aa96651

logging/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/rs/zerolog"
1010
)
1111

12+
// TODO: Remove this once we have a proper hooks package.
1213
// This is duplicated from the network package, because import cycles are not allowed.
1314
type (
1415
Signature map[string]interface{}

0 commit comments

Comments
 (0)