Skip to content

Commit

Permalink
Set max Postgres DB Connections (#1878)
Browse files Browse the repository at this point in the history
* set max connections on postgres db
  • Loading branch information
badgersrus authored Apr 12, 2024
1 parent 4122467 commit 261253b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go/host/storage/init/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

const (
defaultDatabase = "postgres"
maxDBPoolSize = 100
)

func CreatePostgresDBConnection(baseURL string, dbName string) (*sql.DB, error) {
Expand Down Expand Up @@ -46,6 +47,7 @@ func CreatePostgresDBConnection(baseURL string, dbName string) (*sql.DB, error)
dbURL = fmt.Sprintf("%s%s", baseURL, dbName)

db, err = sql.Open("postgres", dbURL)
db.SetMaxOpenConns(maxDBPoolSize)
if err != nil {
return nil, fmt.Errorf("failed to connect to PostgreSQL database %s: %v", dbName, err)
}
Expand Down
25 changes: 25 additions & 0 deletions integration/simulation/p2p/in_mem_obscuro_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (c *inMemObscuroClient) Call(result interface{}, method string, args ...int
case rpc.GetRollupListing:
return c.getRollupListing(result, args)

case rpc.GetPublicTransactionData:
return c.getPublicTransactionData(result, args)

default:
return fmt.Errorf("RPC method %s is unknown", method)
}
Expand Down Expand Up @@ -369,6 +372,28 @@ func (c *inMemObscuroClient) getRollupListing(result interface{}, args []interfa
return nil
}

func (c *inMemObscuroClient) getPublicTransactionData(result interface{}, args []interface{}) error {
if len(args) != 1 {
return fmt.Errorf("expected 1 arg to %s, got %d", rpc.GetPublicTransactionData, len(args))
}
pagination, ok := args[0].(*common.QueryPagination)
if !ok {
return fmt.Errorf("first arg to %s is of type %T, expected type int", rpc.GetPublicTransactionData, args[0])
}

txs, err := c.tenScanAPI.GetPublicTransactionData(pagination)
if err != nil {
return fmt.Errorf("`%s` call failed. Cause: %w", rpc.GetPublicTransactionData, err)
}

res, ok := result.(*common.TransactionListingResponse)
if !ok {
return fmt.Errorf("result is of type %T, expected *common.BatchListingResponseDeprecated", result)
}
*res = *txs
return nil
}

// getEncryptedBytes expects args to have a single element and it to be of type bytes (client doesn't know anything about what's getting passed through on sensitive methods)
func getEncryptedBytes(args []interface{}, methodName string) ([]byte, error) {
if len(args) != 1 {
Expand Down
51 changes: 51 additions & 0 deletions integration/simulation/validate_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,11 @@ func checkTenscan(t *testing.T, s *Simulation) {
checkTotalTransactions(t, client, idx)
checkForLatestBatches(t, client, idx)
checkForLatestRollups(t, client, idx)

txHashes := getLatestTransactions(t, client, idx)
for _, txHash := range txHashes {
checkBatchFromTxs(t, client, txHash, idx)
}
}
}

Expand Down Expand Up @@ -762,3 +767,49 @@ func checkForLatestRollups(t *testing.T, client rpc.Client, nodeIdx int) {
t.Errorf("node %d: expected at least %d transactions, but only received %d", nodeIdx, 5, len(latestRollups.RollupsData))
}
}

func getLatestTransactions(t *testing.T, client rpc.Client, nodeIdx int) []gethcommon.Hash {
var transactionResponse common.TransactionListingResponse
var txHashes []gethcommon.Hash
pagination := common.QueryPagination{Offset: uint64(0), Size: uint(5)}
err := client.Call(&transactionResponse, rpc.GetPublicTransactionData, &pagination)
if err != nil {
t.Errorf("node %d: could not retrieve latest transactions. Cause: %s", nodeIdx, err)
}

for _, transaction := range transactionResponse.TransactionsData {
txHashes = append(txHashes, transaction.TransactionHash)
}

return txHashes
}

// Retrieves the batch using the transaction hash, and validates it.
func checkBatchFromTxs(t *testing.T, client rpc.Client, txHash gethcommon.Hash, nodeIdx int) {
var batchByTx *common.ExtBatch
err := client.Call(&batchByTx, rpc.GetBatchByTx, txHash)
if err != nil {
t.Errorf("node %d: could not retrieve batch for transaction. Cause: %s", nodeIdx, err)
return
}

var containsTx bool
for _, txHashFromBatch := range batchByTx.TxHashes {
if txHashFromBatch == txHash {
containsTx = true
}
}
if !containsTx {
t.Errorf("node %d: retrieved batch by transaction, but transaction was missing from batch", nodeIdx)
}

var batchByHash *common.ExtBatch
err = client.Call(&batchByHash, rpc.GetBatch, batchByTx.Header.Hash())
if err != nil {
t.Errorf("node %d: could not retrieve batch by hash. Cause: %s", nodeIdx, err)
return
}
if batchByHash.Header.Hash() != batchByTx.Header.Hash() {
t.Errorf("node %d: retrieved batch by hash, but hash was incorrect", nodeIdx)
}
}

0 comments on commit 261253b

Please sign in to comment.