Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add optional dispose method #113

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 26 additions & 1 deletion datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"
"net/http"
"strings"
"sync"
"time"

"github.com/grafana/grafana-plugin-sdk-go/data/sqlutil"

"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
Expand Down Expand Up @@ -111,6 +112,24 @@ func NewDatasource(c Driver) *SQLDatasource {
// Dispose cleans up datasource instance resources.
// Note: Called when testing and saving a datasource
func (ds *SQLDatasource) Dispose() {
if ds.driverSettings.Dispose {
var connections []dbConnection
ds.dbConnections.Range(func(key, value any) bool {
connection, ok := value.(dbConnection)
if ok {
connections = append(connections, connection)
}
return true
})
backend.Logger.Debug(fmt.Sprintf("Disposing %d connections", len(connections)))
for _, conn := range connections {
if err := conn.db.Close(); err != nil {
backend.Logger.Warn(fmt.Sprintf("closing connection failed: %s", err.Error()))
}
k := defaultKey(getDatasourceUID(conn.settings))
ds.dbConnections.Delete(k)
}
}
}

// QueryData creates the Responses list and executes each query
Expand Down Expand Up @@ -150,6 +169,12 @@ func (ds *SQLDatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
wg.Wait()

errs := ds.errors(response)

// Dispose after alerts and other non-user queries
if req.PluginContext.User == nil && ds.driverSettings.Dispose {
ds.Dispose()
}

if ds.driverSettings.Errors {
return response.Response(), errs
}
Expand Down
11 changes: 11 additions & 0 deletions datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ func Test_no_errors(t *testing.T) {
assert.Equal(t, expected, result.Message)
}

func TestDispose(t *testing.T) {
req, _, ds := healthRequest(t, "pass", test.DriverOpts{}, `{ "dispose": true} `)
_, err := ds.CheckHealth(context.Background(), &req)
assert.Nil(t, err)

ds.Dispose()

_, err = ds.CheckHealth(context.Background(), &req)
assert.NotNil(t, err)
}

func queryRequest(t *testing.T, name string, opts test.DriverOpts, cfg string) (*backend.QueryDataRequest, *test.SqlHandler, *sqlds.SQLDatasource) {
driver, handler := test.NewDriver(name, test.Data{}, nil, opts)
ds := sqlds.NewDatasource(driver)
Expand Down
1 change: 1 addition & 0 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type DriverSettings struct {
RetryOn []string
ForwardHeaders bool
Errors bool
Dispose bool
}

// Driver is a simple interface that defines how to connect to a backend SQL datasource
Expand Down