Skip to content

Commit

Permalink
Fix linting errrors - 6
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Jun 21, 2024
1 parent ab4f82e commit 569ca78
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
16 changes: 10 additions & 6 deletions cmd/soroban-rpc/internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func openSQLiteDB(dbFilePath string) (*db.Session, error) {
// 2. Disable WAL auto-checkpointing (we will do the checkpointing ourselves with wal_checkpoint pragmas
// after every write transaction).
// 3. Use synchronous=NORMAL, which is faster and still safe in WAL mode.
session, err := db.Open("sqlite3", fmt.Sprintf("file:%s?_journal_mode=WAL&_wal_autocheckpoint=0&_synchronous=NORMAL", dbFilePath))
session, err := db.Open("sqlite3",
fmt.Sprintf("file:%s?_journal_mode=WAL&_wal_autocheckpoint=0&_synchronous=NORMAL", dbFilePath))
if err != nil {
return nil, fmt.Errorf("open failed: %w", err)
}
Expand All @@ -78,7 +79,9 @@ func openSQLiteDB(dbFilePath string) (*db.Session, error) {
return session, nil
}

func OpenSQLiteDBWithPrometheusMetrics(dbFilePath string, namespace string, sub db.Subservice, registry *prometheus.Registry) (*DB, error) {
func OpenSQLiteDBWithPrometheusMetrics(dbFilePath string, namespace string, sub db.Subservice,
registry *prometheus.Registry,
) (*DB, error) {
session, err := openSQLiteDB(dbFilePath)
if err != nil {
return nil, err
Expand Down Expand Up @@ -133,7 +136,8 @@ func getMetaValue(ctx context.Context, q db.SessionInterface, key string) (strin
case 1:
// expected length on an initialized DB
default:
return "", fmt.Errorf("multiple entries (%d) for key %q in table %q", len(results), latestLedgerSequenceMetaKey, metaTableName)
return "", fmt.Errorf("multiple entries (%d) for key %q in table %q", len(results),
latestLedgerSequenceMetaKey, metaTableName)
}
return results[0], nil
}
Expand Down Expand Up @@ -330,11 +334,11 @@ func (w writeTx) Rollback() error {
// errors.New("not in transaction") is returned when rolling back a transaction which has
// already been committed or rolled back. We can ignore those errors
// because we allow rolling back after commits in defer statements.
if err := w.tx.Rollback(); err == nil || err.Error() == "not in transaction" {
err := w.tx.Rollback()
if err == nil || err.Error() == "not in transaction" {
return nil
} else {
return err
}
return err
}

func runSQLMigrations(db *sql.DB, dialect string) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"testing"
"time"

"github.com/stellar/go/network"
"github.com/stretchr/testify/require"

"github.com/stellar/go/clients/stellarcore"
"github.com/stellar/go/keypair"
"github.com/stellar/go/network"
proto "github.com/stellar/go/protocols/stellarcore"
supportlog "github.com/stellar/go/support/log"
"github.com/stellar/go/txnbuild"
Expand Down Expand Up @@ -385,7 +385,7 @@ func (i *Test) generateCaptiveCoreCfgForContainer() {
// Try the directory before the integration test refactoring
// TODO: remove this hack after protocol 22 is released
out, err = getOldVersionCaptiveCoreConfigVersion("../../test", captiveCoreConfigFilename)
outStr := strings.Replace(string(out), `ADDRESS="localhost"`, `ADDRESS="${CORE_HOST_PORT}"`, -1)
outStr := strings.ReplaceAll(string(out), `ADDRESS="localhost"`, `ADDRESS="${CORE_HOST_PORT}"`)
out = []byte(outStr)
}
require.NoError(i.t, err)
Expand Down
43 changes: 28 additions & 15 deletions cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"strings"
Expand All @@ -29,7 +30,10 @@ import (
// maxHTTPRequestSize defines the largest request size that the http handler
// would be willing to accept before dropping the request. The implementation
// uses the default MaxBytesHandler to limit the request size.
const maxHTTPRequestSize = 512 * 1024 // half a megabyte
const (
maxHTTPRequestSize = 512 * 1024 // half a megabyte
warningThresholdDenominator = 3
)

// Handler is the HTTP handler which serves the Soroban JSON RPC responses
type Handler struct {
Expand Down Expand Up @@ -67,7 +71,7 @@ func decorateHandlers(daemon interfaces.Daemon, logger *log.Entry, m handler.Map
}, []string{"endpoint", "status"})
decorated := handler.Map{}
for endpoint, h := range m {
// create copy of h so it can be used in closure bleow
// create copy of h, so it can be used in closure below
h := h
decorated[endpoint] = handler.New(func(ctx context.Context, r *jrpc2.Request) (interface{}, error) {
reqID := strconv.FormatUint(middleware.NextRequestID(), 10)
Expand All @@ -80,7 +84,8 @@ func decorateHandlers(daemon interfaces.Daemon, logger *log.Entry, m handler.Map
if ok && simulateTransactionResponse.Error != "" {
label["status"] = "error"
} else if err != nil {
if jsonRPCErr, ok := err.(*jrpc2.Error); ok {
var jsonRPCErr *jrpc2.Error
if errors.As(err, &jsonRPCErr) {
prometheusLabelReplacer := strings.NewReplacer(" ", "_", "-", "_", "(", "", ")", "")
status := prometheusLabelReplacer.Replace(jsonRPCErr.Code.String())
label["status"] = status
Expand Down Expand Up @@ -139,7 +144,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {

// Get the largest history window
var ledgerRangeGetter db.LedgerRangeGetter = params.EventStore
var retentionWindow = cfg.EventLedgerRetentionWindow
retentionWindow := cfg.EventLedgerRetentionWindow
if cfg.TransactionLedgerRetentionWindow > cfg.EventLedgerRetentionWindow {
retentionWindow = cfg.TransactionLedgerRetentionWindow
ledgerRangeGetter = params.TransactionReader
Expand Down Expand Up @@ -176,8 +181,9 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
requestDurationLimit: cfg.MaxGetNetworkExecutionDuration,
},
{
methodName: "getVersionInfo",
underlyingHandler: methods.NewGetVersionInfoHandler(params.Logger, params.LedgerEntryReader, params.LedgerReader, params.Daemon),
methodName: "getVersionInfo",
underlyingHandler: methods.NewGetVersionInfoHandler(params.Logger, params.LedgerEntryReader,
params.LedgerReader, params.Daemon),
longName: "get_version_info",
queueLimit: cfg.RequestBacklogGetVersionInfoQueueLimit,
requestDurationLimit: cfg.MaxGetVersionInfoExecutionDuration,
Expand Down Expand Up @@ -211,8 +217,9 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
requestDurationLimit: cfg.MaxGetTransactionExecutionDuration,
},
{
methodName: "getTransactions",
underlyingHandler: methods.NewGetTransactionsHandler(params.Logger, params.LedgerReader, params.TransactionReader, cfg.MaxTransactionsLimit, cfg.DefaultTransactionsLimit, cfg.NetworkPassphrase),
methodName: "getTransactions",
underlyingHandler: methods.NewGetTransactionsHandler(params.Logger, params.LedgerReader,
params.TransactionReader, cfg.MaxTransactionsLimit, cfg.DefaultTransactionsLimit, cfg.NetworkPassphrase),
longName: "get_transactions",
queueLimit: cfg.RequestBacklogGetTransactionsQueueLimit,
requestDurationLimit: cfg.MaxGetTransactionsExecutionDuration,
Expand Down Expand Up @@ -260,8 +267,10 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {

durationWarnCounterName := handler.longName + "_execution_threshold_warning"
durationLimitCounterName := handler.longName + "_execution_threshold_limit"
durationWarnCounterHelp := "The metric measures the count of " + handler.methodName + " requests that surpassed the warning threshold for execution time"
durationLimitCounterHelp := "The metric measures the count of " + handler.methodName + " requests that surpassed the limit threshold for execution time"
durationWarnCounterHelp := "The metric measures the count of " + handler.methodName +
" requests that surpassed the warning threshold for execution time"
durationLimitCounterHelp := "The metric measures the count of " + handler.methodName +
" requests that surpassed the limit threshold for execution time"

requestDurationWarnCounter := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: params.Daemon.MetricsNamespace(), Subsystem: "network",
Expand All @@ -274,7 +283,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
Help: durationLimitCounterHelp,
})
// set the warning threshold to be one third of the limit.
requestDurationWarn := handler.requestDurationLimit / 3
requestDurationWarn := handler.requestDurationLimit / warningThresholdDenominator
durationLimiter := network.MakeJrpcRequestDurationLimiter(
queueLimiter.Handle,
requestDurationWarn,
Expand Down Expand Up @@ -303,12 +312,16 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
params.Logger)

globalQueueRequestExecutionDurationWarningCounter := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: params.Daemon.MetricsNamespace(), Subsystem: "network", Name: "global_request_execution_duration_threshold_warning",
Help: "The metric measures the count of requests that surpassed the warning threshold for execution time",
Namespace: params.Daemon.MetricsNamespace(),
Subsystem: "network",
Name: "global_request_execution_duration_threshold_warning",
Help: "The metric measures the count of requests that surpassed the warning threshold for execution time",
})
globalQueueRequestExecutionDurationLimitCounter := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: params.Daemon.MetricsNamespace(), Subsystem: "network", Name: "global_request_execution_duration_threshold_limit",
Help: "The metric measures the count of requests that surpassed the limit threshold for execution time",
Namespace: params.Daemon.MetricsNamespace(),
Subsystem: "network",
Name: "global_request_execution_duration_threshold_limit",
Help: "The metric measures the count of requests that surpassed the limit threshold for execution time",
})
handler := network.MakeHTTPRequestDurationLimiter(
queueLimitedBridge,
Expand Down

0 comments on commit 569ca78

Please sign in to comment.