Skip to content

Commit

Permalink
Remove UpdateCacheRequest wrapper struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-soltanpour committed Dec 19, 2023
1 parent 005cf8b commit fadcf85
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gatewayd-io/gatewayd-plugin-sdk/logging"
"github.com/gatewayd-io/gatewayd-plugin-sdk/metrics"
p "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/getsentry/sentry-go"
"github.com/go-redis/redis/v8"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -56,7 +57,7 @@ func main() {
cacheBufferSize = 100 // default value
}

pluginInstance.Impl.UpdateCacheChannel = make(chan plugin.UpdateCacheRequest, cacheBufferSize)
pluginInstance.Impl.UpdateCacheChannel = make(chan *v1.Struct, cacheBufferSize)
go pluginInstance.Impl.UpdateCache(context.Background())

pluginInstance.Impl.RedisURL = cast.ToString(cfg["redisURL"])
Expand Down
15 changes: 6 additions & 9 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Plugin struct {
ScanCount int64
ExitOnStartupError bool

UpdateCacheChannel chan UpdateCacheRequest
UpdateCacheChannel chan *v1.Struct

// Periodic invalidator configuration.
PeriodicInvalidatorEnabled bool
Expand Down Expand Up @@ -146,18 +146,17 @@ func (p *Plugin) OnTrafficFromClient(
return req, nil
}

type UpdateCacheRequest struct {
serverResponse *v1.Struct
}
//type UpdateCacheRequest struct {
// serverResponse *v1.Struct
//}

func (p *Plugin) UpdateCache(ctx context.Context) {
for {
updateCacheRequest, ok := <-p.UpdateCacheChannel
serverResponse, ok := <-p.UpdateCacheChannel
if !ok {
p.Logger.Info("Channel closed, returning from function")
return
}
serverResponse := updateCacheRequest.serverResponse

OnTrafficFromServerCounter.Inc()
resp, err := postgres.HandleServerMessage(serverResponse, p.Logger)
Expand Down Expand Up @@ -247,9 +246,7 @@ func (p *Plugin) OnTrafficFromServer(
_ context.Context, resp *v1.Struct,
) (*v1.Struct, error) {
p.Logger.Info("Traffic is coming from the server side")
p.UpdateCacheChannel <- UpdateCacheRequest{
serverResponse: resp,
}
p.UpdateCacheChannel <- resp
return resp, nil
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Test_Plugin(t *testing.T) {
redisClient := redis.NewClient(redisConfig)
assert.NotNil(t, redisClient)

updateCacheChannel := make(chan UpdateCacheRequest, 10)
updateCacheChannel := make(chan *v1.Struct, 10)

// Create and initialize a new plugin.
logger := hclog.New(&hclog.LoggerOptions{
Expand Down

0 comments on commit fadcf85

Please sign in to comment.