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

all: Refactor db.Session to take ctx into each call, instead of storing it on the session level #3545

Merged
merged 6 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions exp/services/captivecore/internal/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"context"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -35,6 +36,7 @@ func (s *ServerTestSuite) SetupTest() {
s.server = httptest.NewServer(s.handler)
var err error
s.client, err = ledgerbackend.NewRemoteCaptive(
context.Background(),
s.server.URL,
ledgerbackend.PrepareRangePollInterval(time.Millisecond),
)
Expand Down
3 changes: 2 additions & 1 deletion exp/services/captivecore/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"go/types"
"strings"
Expand Down Expand Up @@ -138,7 +139,7 @@ func main() {
if err != nil {
logger.WithError(err).Fatal("Could not create db connection instance")
}
captiveConfig.LedgerHashStore = ledgerbackend.NewHorizonDBLedgerHashStore(dbConn)
captiveConfig.LedgerHashStore = ledgerbackend.NewHorizonDBLedgerHashStore(context.Background(), dbConn)
}

core, err := ledgerbackend.NewCaptive(captiveConfig)
Expand Down
2 changes: 1 addition & 1 deletion exp/tools/dump-ledger-state/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (processor csvProcessor) ProcessChange(change ingest.Change) error {
if !ok {
return nil
}
if err := processor.changeStats.ProcessChange(change); err != nil {
if err := processor.changeStats.ProcessChange(context.Background(), change); err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions handlers/federation/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

switch typ {
case "name":
h.lookupByName(w, q)
h.lookupByName(w, r, q)
case "id":
h.lookupByID(w, q)
h.lookupByID(w, r, q)
case "forward":
h.lookupByForward(w, r.URL.Query())
case "txid":
Expand All @@ -55,7 +55,7 @@ func (h *Handler) failNotImplemented(w http.ResponseWriter, msg string) {
}, http.StatusNotImplemented)
}

func (h *Handler) lookupByID(w http.ResponseWriter, q string) {
func (h *Handler) lookupByID(w http.ResponseWriter, r *http.Request, q string) {
rd, ok := h.Driver.(ReverseDriver)

if !ok {
Expand All @@ -65,7 +65,7 @@ func (h *Handler) lookupByID(w http.ResponseWriter, q string) {

// TODO: validate that `q` is a strkey encoded address

rec, err := rd.LookupReverseRecord(q)
rec, err := rd.LookupReverseRecord(r.Context(), q)
if err != nil {
h.writeError(w, errors.Wrap(err, "lookup record"))
return
Expand All @@ -81,7 +81,7 @@ func (h *Handler) lookupByID(w http.ResponseWriter, q string) {
}, http.StatusOK)
}

func (h *Handler) lookupByName(w http.ResponseWriter, q string) {
func (h *Handler) lookupByName(w http.ResponseWriter, r *http.Request, q string) {
name, domain, err := address.Split(q)
if err != nil {
h.writeJSON(w, ErrorResponse{
Expand All @@ -91,7 +91,7 @@ func (h *Handler) lookupByName(w http.ResponseWriter, q string) {
return
}

rec, err := h.Driver.LookupRecord(name, domain)
rec, err := h.Driver.LookupRecord(r.Context(), name, domain)
if err != nil {
h.writeError(w, errors.Wrap(err, "lookupByName"))
return
Expand Down
3 changes: 2 additions & 1 deletion handlers/federation/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package federation

import (
"context"
"net/http"
"net/url"
"testing"
Expand Down Expand Up @@ -200,7 +201,7 @@ func (fd ForwardTestDriver) LookupForwardingRecord(query url.Values) (*Record, e
}
}

func (fd ForwardTestDriver) LookupRecord(name string, domain string) (*Record, error) {
func (fd ForwardTestDriver) LookupRecord(ctx context.Context, name string, domain string) (*Record, error) {
return nil, nil
}

Expand Down
5 changes: 3 additions & 2 deletions handlers/federation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package federation

import (
"context"
"database/sql"
"net/url"
"sync"
Expand All @@ -25,7 +26,7 @@ type Driver interface {
// federation request to lookup a `Record` using the provided stellar address.
// An implementation should return a nil `*Record` value if the lookup
// successfully executed but no result was found.
LookupRecord(name string, domain string) (*Record, error)
LookupRecord(ctx context.Context, name, domain string) (*Record, error)
}

// ErrorResponse represents the JSON response sent to a client when the request
Expand Down Expand Up @@ -65,7 +66,7 @@ type ReverseDriver interface {
// federation request to lookup a `ReverseRecord` using the provided strkey
// encoded accountID. An implementation should return a nil `*ReverseRecord`
// value if the lookup successfully executed but no result was found.
LookupReverseRecord(accountID string) (*ReverseRecord, error)
LookupReverseRecord(ctx context.Context, accountID string) (*ReverseRecord, error)
}

// ForwardDriver represents a data source against which forward queries can
Expand Down
9 changes: 7 additions & 2 deletions handlers/federation/reverse_sql_driver.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package federation

import "github.com/stellar/go/support/errors"
import (
"context"

"github.com/stellar/go/support/errors"
)

// LookupReverseRecord implements `ReverseDriver` by performing
// `drv.LookupReverseRecordQuery` against `drv.DB` using the provided parameter
func (drv *ReverseSQLDriver) LookupReverseRecord(
ctx context.Context,
accountid string,
) (*ReverseRecord, error) {
drv.initDB()
var result ReverseRecord

err := drv.db.GetRaw(&result, drv.LookupReverseRecordQuery, accountid)
err := drv.db.GetRaw(ctx, &result, drv.LookupReverseRecordQuery, accountid)

if drv.db.NoRows(err) {
return nil, nil
Expand Down
12 changes: 8 additions & 4 deletions handlers/federation/sql_driver.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package federation

import "github.com/stellar/go/support/db"
import "github.com/stellar/go/support/errors"
import (
"context"

"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
)

// LookupRecord implements `Driver` by performing `drv.LookupRecordQuery`
// against `drv.DB` using the provided parameters
func (drv *SQLDriver) LookupRecord(name, domain string) (*Record, error) {
func (drv *SQLDriver) LookupRecord(ctx context.Context, name, domain string) (*Record, error) {
drv.initDB()
var result Record

err := drv.db.GetRaw(&result, drv.LookupRecordQuery, name, domain)
err := drv.db.GetRaw(ctx, &result, drv.LookupRecordQuery, name, domain)

if drv.db.NoRows(err) {
return nil, nil
Expand Down
7 changes: 7 additions & 0 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,8 +650,15 @@ func (c *CaptiveStellarCore) Close() error {
// after the CaptiveStellarCore context is canceled all subsequent calls to PrepareRange() will fail
c.cancel()

// TODO: Sucks to ignore the error here, but no worse than it was before,
// so...
if c.ledgerHashStore != nil {
c.ledgerHashStore.Close()
}

if c.stellarCoreRunner != nil {
return c.stellarCoreRunner.close()
}

return nil
}
Loading