Skip to content

Commit

Permalink
Merge pull request #195 from gatewayd-io/use-enum-for-hook-names
Browse files Browse the repository at this point in the history
Use enum for hook names
  • Loading branch information
mostafa authored Mar 7, 2023
2 parents a37a540 + 57df0c8 commit 0f1a0e0
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 110 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ linters:
- funlen
- maintidx
- musttag
- nosnakecase
8 changes: 7 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func (a *API) GetPlugins(context.Context, *emptypb.Empty) (*v1.PluginConfigs, er
requires[r.Name] = r.Version
}
}

hooks := make([]int32, 0)
for _, hook := range plugIn.Hooks {
hooks = append(hooks, int32(hook.Number()))
}

plugins = append(plugins, &v1.PluginConfig{
Id: &v1.PluginID{
Name: pluginID.Name,
Expand All @@ -96,7 +102,7 @@ func (a *API) GetPlugins(context.Context, *emptypb.Empty) (*v1.PluginConfigs, er
License: plugIn.License,
ProjectUrl: plugIn.ProjectURL,
Config: plugIn.Config,
Hooks: plugIn.Hooks,
Hooks: hooks,
Requires: requires,
Tags: plugIn.Tags,
Categories: plugIn.Categories,
Expand Down
6 changes: 3 additions & 3 deletions api/v1/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/v1/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ message PluginConfig {
// internal and external config options
map<string, string> config = 6;
// hooks it attaches to
repeated string hooks = 7;
repeated int32 hooks = 7;
// required plugins
map<string, string> requires = 8;
repeated string tags = 9;
Expand All @@ -103,4 +103,4 @@ message PluginConfig {

message PluginConfigs {
repeated PluginConfig configs = 1;
}
}
3 changes: 2 additions & 1 deletion api/v1/api.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@
"hooks": {
"type": "array",
"items": {
"type": "string"
"type": "integer",
"format": "int32"
},
"title": "hooks it attaches to"
},
Expand Down
19 changes: 12 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/NYTimes/gziphandler"
sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/gatewayd-io/gatewayd/api"
"github.com/gatewayd-io/gatewayd/config"
gerr "github.com/gatewayd-io/gatewayd/errors"
Expand Down Expand Up @@ -214,7 +215,7 @@ var runCmd = &cobra.Command{
updatedGlobalConfig, err := pluginRegistry.Run(
context.Background(),
conf.GlobalKoanf.All(),
sdkPlugin.OnConfigLoaded)
v1.HookName_HOOK_NAME_ON_CONFIG_LOADED)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnConfigLoaded hooks")
span.RecordError(err)
Expand Down Expand Up @@ -296,7 +297,8 @@ var runCmd = &cobra.Command{
// This is a notification hook, so we don't care about the result.
// TODO: Use a context with a timeout
if data, ok := conf.GlobalKoanf.Get("loggers").(map[string]interface{}); ok {
_, err = pluginRegistry.Run(context.Background(), data, sdkPlugin.OnNewLogger)
_, err = pluginRegistry.Run(
context.Background(), data, v1.HookName_HOOK_NAME_ON_NEW_LOGGER)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewLogger hooks")
span.RecordError(err)
Expand Down Expand Up @@ -364,7 +366,8 @@ var runCmd = &cobra.Command{
"tcpKeepAlive": client.TCPKeepAlive,
"tcpKeepAlivePeriod": client.TCPKeepAlivePeriod.String(),
}
_, err := pluginRegistry.Run(context.Background(), clientCfg, sdkPlugin.OnNewClient)
_, err := pluginRegistry.Run(
context.Background(), clientCfg, v1.HookName_HOOK_NAME_ON_NEW_CLIENT)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewClient hooks")
span.RecordError(err)
Expand Down Expand Up @@ -399,7 +402,7 @@ var runCmd = &cobra.Command{
_, err = pluginRegistry.Run(
context.Background(),
map[string]interface{}{"name": name, "size": cfg.GetSize()},
sdkPlugin.OnNewPool)
v1.HookName_HOOK_NAME_ON_NEW_POOL)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewPool hooks")
span.RecordError(err)
Expand Down Expand Up @@ -432,7 +435,8 @@ var runCmd = &cobra.Command{
))

if data, ok := conf.GlobalKoanf.Get("proxy").(map[string]interface{}); ok {
_, err = pluginRegistry.Run(context.Background(), data, sdkPlugin.OnNewProxy)
_, err = pluginRegistry.Run(
context.Background(), data, v1.HookName_HOOK_NAME_ON_NEW_PROXY)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewProxy hooks")
span.RecordError(err)
Expand Down Expand Up @@ -507,7 +511,8 @@ var runCmd = &cobra.Command{
))

if data, ok := conf.GlobalKoanf.Get("servers").(map[string]interface{}); ok {
_, err = pluginRegistry.Run(context.Background(), data, sdkPlugin.OnNewServer)
_, err = pluginRegistry.Run(
context.Background(), data, v1.HookName_HOOK_NAME_ON_NEW_SERVER)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnNewServer hooks")
span.RecordError(err)
Expand Down Expand Up @@ -617,7 +622,7 @@ var runCmd = &cobra.Command{
_, err := pluginRegistry.Run(
context.Background(),
map[string]interface{}{"signal": sig.String()},
sdkPlugin.OnSignal,
v1.HookName_HOOK_NAME_ON_SIGNAL,
)
if err != nil {
logger.Error().Err(err).Msg("Failed to run OnSignal hooks")
Expand Down
3 changes: 0 additions & 3 deletions config/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ var (
"panic": zerolog.PanicLevel,
"disabled": zerolog.Disabled,
}
//nolint:nosnakecase
rSyslogPriorities = map[string]syslog.Priority{
"emerg": syslog.LOG_EMERG,
"alert": syslog.LOG_ALERT,
Expand Down Expand Up @@ -176,8 +175,6 @@ func (l Logger) GetLevel() zerolog.Level {
}

// GetSyslogPriority returns the rsyslog facility from config file.
//
//nolint:nosnakecase
func (l Logger) GetSyslogPriority() syslog.Priority {
if priority, ok := rSyslogPriorities[l.SyslogPriority]; ok {
return priority | syslog.LOG_DAEMON
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.0
github.com/NYTimes/gziphandler v1.1.1
github.com/envoyproxy/protoc-gen-validate v0.9.1
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.9
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.13
github.com/getsentry/sentry-go v0.19.0
github.com/go-co-op/gocron v1.18.1
github.com/google/go-cmp v0.5.9
Expand All @@ -28,7 +28,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0
go.opentelemetry.io/otel/sdk v1.14.0
go.opentelemetry.io/otel/trace v1.14.0
golang.org/x/exp v0.0.0-20230306221820-f0f767cdffd6
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.28.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.9 h1:AHvkO38U6t2GYWFvsU9aL2eymff3rw2UfRjgUlmVVXo=
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.9/go.mod h1:8ubBVLIi3KM+Xpq3/PHKtD5ev5wtcEobNTSKbvQaUmE=
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.13 h1:XiCVN09Xc/lcVIGcH+wtDU97V2r317Fchiri94HccwY=
github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.13/go.mod h1:8ubBVLIi3KM+Xpq3/PHKtD5ev5wtcEobNTSKbvQaUmE=
github.com/getsentry/sentry-go v0.19.0 h1:BcCH3CN5tXt5aML+gwmbFwVptLLQA+eT866fCO9wVOM=
github.com/getsentry/sentry-go v0.19.0/go.mod h1:y3+lGEFEFexZtpbG1GUE2WD/f9zGyKYwpEqryTOC/nE=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -463,8 +463,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20230306221820-f0f767cdffd6 h1:3p+wVC0x0TCIPgd3LCQlpgVlEtjziEC5v42w7+B8t8M=
golang.org/x/exp v0.0.0-20230306221820-f0f767cdffd6/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
10 changes: 5 additions & 5 deletions network/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"time"

sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/gatewayd-io/gatewayd/config"
gerr "github.com/gatewayd-io/gatewayd/errors"
"github.com/gatewayd-io/gatewayd/metrics"
Expand Down Expand Up @@ -293,7 +293,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError {
},
},
origErr),
sdkPlugin.OnTrafficFromClient)
v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT)
if err != nil {
pr.logger.Error().Err(err).Msg("Error running hook")
span.RecordError(err)
Expand Down Expand Up @@ -337,7 +337,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError {
},
},
err),
sdkPlugin.OnTrafficToServer)
v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_SERVER)
if err != nil {
pr.logger.Error().Err(err).Msg("Error running hook")
span.RecordError(err)
Expand Down Expand Up @@ -398,7 +398,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError {
},
},
err),
sdkPlugin.OnTrafficFromServer)
v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_SERVER)
if err != nil {
pr.logger.Error().Err(err).Msg("Error running hook")
span.RecordError(err)
Expand Down Expand Up @@ -434,7 +434,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError {
},
err,
),
sdkPlugin.OnTrafficToClient)
v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_CLIENT)
if err != nil {
pr.logger.Error().Err(err).Msg("Error running hook")
span.RecordError(err)
Expand Down
28 changes: 17 additions & 11 deletions network/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"time"

sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/gatewayd-io/gatewayd/config"
gerr "github.com/gatewayd-io/gatewayd/errors"
"github.com/gatewayd-io/gatewayd/metrics"
Expand Down Expand Up @@ -49,7 +49,7 @@ func (s *Server) OnBoot(engine gnet.Engine) gnet.Action {
_, err := s.pluginRegistry.Run(
context.Background(),
map[string]interface{}{"status": fmt.Sprint(s.Status)},
sdkPlugin.OnBooting)
v1.HookName_HOOK_NAME_ON_BOOTING)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnBooting hook")
span.RecordError(err)
Expand All @@ -65,7 +65,7 @@ func (s *Server) OnBoot(engine gnet.Engine) gnet.Action {
_, err = s.pluginRegistry.Run(
context.Background(),
map[string]interface{}{"status": fmt.Sprint(s.Status)},
sdkPlugin.OnBooted)
v1.HookName_HOOK_NAME_ON_BOOTED)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnBooted hook")
span.RecordError(err)
Expand Down Expand Up @@ -93,7 +93,8 @@ func (s *Server) OnOpen(gconn gnet.Conn) ([]byte, gnet.Action) {
"remote": gconn.RemoteAddr().String(),
},
}
_, err := s.pluginRegistry.Run(context.Background(), onOpeningData, sdkPlugin.OnOpening)
_, err := s.pluginRegistry.Run(
context.Background(), onOpeningData, v1.HookName_HOOK_NAME_ON_OPENING)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnOpening hook")
span.RecordError(err)
Expand Down Expand Up @@ -138,7 +139,8 @@ func (s *Server) OnOpen(gconn gnet.Conn) ([]byte, gnet.Action) {
"remote": gconn.RemoteAddr().String(),
},
}
_, err = s.pluginRegistry.Run(context.Background(), onOpenedData, sdkPlugin.OnOpened)
_, err = s.pluginRegistry.Run(
context.Background(), onOpenedData, v1.HookName_HOOK_NAME_ON_OPENED)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnOpened hook")
span.RecordError(err)
Expand Down Expand Up @@ -171,7 +173,8 @@ func (s *Server) OnClose(gconn gnet.Conn, err error) gnet.Action {
if err != nil {
data["error"] = err.Error()
}
_, gatewaydErr := s.pluginRegistry.Run(context.Background(), data, sdkPlugin.OnClosing)
_, gatewaydErr := s.pluginRegistry.Run(
context.Background(), data, v1.HookName_HOOK_NAME_ON_CLOSING)
if gatewaydErr != nil {
s.logger.Error().Err(gatewaydErr).Msg("Failed to run OnClosing hook")
span.RecordError(gatewaydErr)
Expand Down Expand Up @@ -205,7 +208,8 @@ func (s *Server) OnClose(gconn gnet.Conn, err error) gnet.Action {
if err != nil {
data["error"] = err.Error()
}
_, gatewaydErr = s.pluginRegistry.Run(context.Background(), data, sdkPlugin.OnClosed)
_, gatewaydErr = s.pluginRegistry.Run(
context.Background(), data, v1.HookName_HOOK_NAME_ON_CLOSED)
if gatewaydErr != nil {
s.logger.Error().Err(gatewaydErr).Msg("Failed to run OnClosed hook")
span.RecordError(gatewaydErr)
Expand All @@ -230,7 +234,8 @@ func (s *Server) OnTraffic(gconn gnet.Conn) gnet.Action {
"remote": gconn.RemoteAddr().String(),
},
}
_, err := s.pluginRegistry.Run(context.Background(), onTrafficData, sdkPlugin.OnTraffic)
_, err := s.pluginRegistry.Run(
context.Background(), onTrafficData, v1.HookName_HOOK_NAME_ON_TRAFFIC)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnTraffic hook")
span.RecordError(err)
Expand Down Expand Up @@ -271,7 +276,7 @@ func (s *Server) OnShutdown(engine gnet.Engine) {
_, err := s.pluginRegistry.Run(
context.Background(),
map[string]interface{}{"connections": s.engine.CountConnections()},
sdkPlugin.OnShutdown)
v1.HookName_HOOK_NAME_ON_SHUTDOWN)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnShutdown hook")
span.RecordError(err)
Expand All @@ -298,7 +303,7 @@ func (s *Server) OnTick() (time.Duration, gnet.Action) {
_, err := s.pluginRegistry.Run(
context.Background(),
map[string]interface{}{"connections": s.engine.CountConnections()},
sdkPlugin.OnTick)
v1.HookName_HOOK_NAME_ON_TICK)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run OnTick hook")
span.RecordError(err)
Expand Down Expand Up @@ -334,7 +339,8 @@ func (s *Server) Run() error {
if err != nil && err.Unwrap() != nil {
onRunData["error"] = err.OriginalError.Error()
}
result, err := s.pluginRegistry.Run(context.Background(), onRunData, sdkPlugin.OnRun)
result, err := s.pluginRegistry.Run(
context.Background(), onRunData, v1.HookName_HOOK_NAME_ON_RUN)
if err != nil {
s.logger.Error().Err(err).Msg("Failed to run the hook")
span.RecordError(err)
Expand Down
Loading

0 comments on commit 0f1a0e0

Please sign in to comment.