Skip to content

Commit

Permalink
fix: Fix a memory leak in monitor module
Browse files Browse the repository at this point in the history
Signed-off-by: Utkarsh Saxena <[email protected]>
  • Loading branch information
utk-spartan committed May 27, 2024
1 parent 3c42e25 commit 6cf9eaf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/monitor/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ func (c *Core) isBackendUp(ctx *context.Context, b *gatewayv1.Backend) (bool, er
user: boot.Config.Monitor.Trino.User,
url: url.URL{Scheme: b.GetScheme().Enum().String(), Host: b.GetHostname()},
}
defer trinoClient.Teardown(ctx)
isUp, err := trinoClient.IsClusterUp(ctx)
if err != nil {
provider.Logger(*ctx).WithError(err).Errorw(
Expand Down Expand Up @@ -220,6 +221,7 @@ func (c *Core) getBackendLoad(ctx *context.Context, b *gatewayv1.Backend) (int32
url: url.URL{Scheme: b.GetScheme().Enum().String(), Host: b.GetHostname()},
pass: boot.Config.Monitor.Trino.Password,
}
defer trinoClient.Teardown(ctx)

type StateStat struct {
state string
Expand Down
15 changes: 15 additions & 0 deletions internal/monitor/trino.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
type ITrinoClient interface {
IsClusterUp(ctx *context.Context) (bool, error)
RunQuery(ctx *context.Context, query string) (*sql.Rows, error)
Teardown(ctx *context.Context) error
}

type TrinoClient struct {
Expand Down Expand Up @@ -222,3 +223,17 @@ func (t *TrinoClient) RunQuery(ctx *context.Context, q string) (*sql.Rows, error

return rows, nil
}

func (t *TrinoClient) Teardown(ctx *context.Context) error {
if t.db != nil {
// log
err := t.db.Close()
if err != nil {
provider.Logger(*ctx).WithError(err).Errorw(
"error closing trino db client",
map[string]interface{}{"trinoHost": t.url.Host})
return err
}
}
return nil
}

0 comments on commit 6cf9eaf

Please sign in to comment.