Skip to content

Commit

Permalink
fixes about http request contexts, missing db tx rollback and verbose…
Browse files Browse the repository at this point in the history
… slice contains code
  • Loading branch information
lucasmenendez committed Oct 10, 2023
1 parent 9fa3018 commit 1b3e9aa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 38 deletions.
14 changes: 7 additions & 7 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (capi *census3API) getCensus(msg *api.APIdata, ctx *httprouter.HTTPContext)
return ErrMalformedCensusID
}
censusID := uint64(iCensusID)
internalCtx, cancel := context.WithTimeout(context.Background(), getCensusTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getCensusTimeout)
defer cancel()
currentCensus, err := capi.db.QueriesRO.CensusByID(internalCtx, censusID)
if err != nil {
Expand Down Expand Up @@ -129,7 +129,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str

strategy, err := qtx.StrategyByID(internalCtx, req.StrategyID)
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
if errors.Is(err, sql.ErrNoRows) {
return 0, ErrNotFoundStrategy.WithErr(err)
}
return 0, ErrCantCreateCensus.WithErr(err)
Expand All @@ -152,7 +152,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str
// get the strategy holders from the database
holders, err := qtx.TokenHoldersByStrategyID(internalCtx, req.StrategyID)
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
if errors.Is(err, sql.ErrNoRows) {
return 0, ErrNoStrategyHolders.WithErr(err)
}
return 0, ErrCantCreateCensus.WithErr(err)
Expand All @@ -170,7 +170,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str
// get strategy tokens from the database
strategyTokens, err := qtx.TokensByStrategyID(internalCtx, req.StrategyID)
if err != nil {
if errors.Is(sql.ErrNoRows, err) {
if errors.Is(err, sql.ErrNoRows) {
return 0, ErrNoStrategyTokens.WithErr(err)
}
return 0, ErrCantCreateCensus.WithErr(err)
Expand Down Expand Up @@ -208,7 +208,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str
// check if the census already exists
_, err = qtx.CensusByID(internalCtx, newCensusID)
if err != nil {
if !errors.Is(sql.ErrNoRows, err) {
if !errors.Is(err, sql.ErrNoRows) {
return 0, ErrCantCreateCensus.WithErr(err)
}
} else {
Expand Down Expand Up @@ -282,7 +282,7 @@ func (capi *census3API) enqueueCensus(msg *api.APIdata, ctx *httprouter.HTTPCont
// check if it is not finished or some error occurred
if done && err == nil {
// if everything is ok, get the census information an return it
internalCtx, cancel := context.WithTimeout(context.Background(), enqueueCensusCreationTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), enqueueCensusCreationTimeout)
defer cancel()
censusID, ok := data["censusID"].(uint64)
if !ok {
Expand Down Expand Up @@ -332,7 +332,7 @@ func (capi *census3API) getStrategyCensuses(msg *api.APIdata, ctx *httprouter.HT
return ErrMalformedCensusID.WithErr(err)
}
// get censuses by this strategy ID
internalCtx, cancel := context.WithTimeout(context.Background(), getStrategyCensusesTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getStrategyCensusesTimeout)
defer cancel()
rows, err := capi.db.QueriesRO.CensusByStrategyID(internalCtx, uint64(strategyID))
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions api/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ func (capi *census3API) initStrategiesHandlers() error {
// the database. It returns a 204 response if any strategy is registered or a
// 500 error if something fails.
func (capi *census3API) getStrategies(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
internalCtx, cancel := context.WithTimeout(context.Background(), getStrategiesTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getStrategiesTimeout)
defer cancel()
// create db transaction
tx, err := capi.db.RO.BeginTx(internalCtx, nil)
if err != nil {
return ErrCantGetStrategies.WithErr(err)
}
defer func() {
if err := tx.Rollback(); err != nil && !errors.Is(sql.ErrTxDone, err) {
log.Errorw(err, "get strategies transaction rollback failed")
}
}()
qtx := capi.db.QueriesRO.WithTx(tx)
// get strategies associated to the token provided
rows, err := qtx.ListStrategies(internalCtx)
Expand Down Expand Up @@ -98,7 +103,7 @@ func (capi *census3API) getStrategies(msg *api.APIdata, ctx *httprouter.HTTPCont
}

func (capi *census3API) createStrategy(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
internalCtx, cancel := context.WithTimeout(context.Background(), createDummyStrategyTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), createDummyStrategyTimeout)
defer cancel()

req := CreateStrategyRequest{}
Expand Down Expand Up @@ -195,7 +200,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
}
strategyID := uint64(iStrategyID)
// get strategy from the database
internalCtx, cancel := context.WithTimeout(context.Background(), getStrategyTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getStrategyTimeout)
defer cancel()
strategyData, err := capi.db.QueriesRO.StrategyByID(internalCtx, strategyID)
if err != nil {
Expand Down Expand Up @@ -236,7 +241,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex
// if the provided ID is wrong or empty, a 204 response if the token has not any
// associated strategy or a 500 error if something fails.
func (capi *census3API) getTokenStrategies(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
internalCtx, cancel := context.WithTimeout(context.Background(), getTokensStrategyTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getTokensStrategyTimeout)
defer cancel()
// get the tokenID provided
tokenID := ctx.URLParam("tokenID")
Expand Down
8 changes: 4 additions & 4 deletions api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (capi *census3API) initTokenHandlers() error {
// database. It returns a 204 response if no tokens are registered or a 500
// error if something fails.
func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
internalCtx, cancel := context.WithTimeout(context.Background(), getTokensTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getTokensTimeout)
defer cancel()
// TODO: Support for pagination
// get tokens from the database
Expand Down Expand Up @@ -95,7 +95,7 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex
// init web3 client to get the token information before register in the
// database
w3 := state.Web3{}
internalCtx, cancel := context.WithTimeout(context.Background(), createTokenTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), createTokenTimeout)
defer cancel()
// get correct web3 uri provider
w3URI, exists := capi.w3p.URIByChainID(req.ChainID)
Expand Down Expand Up @@ -193,7 +193,7 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex
// fails.
func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
address := common.HexToAddress(ctx.URLParam("tokenID"))
internalCtx, cancel := context.WithTimeout(context.Background(), getTokenTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getTokenTimeout)
defer cancel()
tokenData, err := capi.db.QueriesRO.TokenByID(internalCtx, address.Bytes())
if err != nil {
Expand Down Expand Up @@ -283,7 +283,7 @@ func (capi *census3API) isTokenHolder(msg *api.APIdata, ctx *httprouter.HTTPCont
if err != nil {
return ErrMalformedChainID.WithErr(err)
}
internalCtx, cancel := context.WithTimeout(context.Background(), getTokenTimeout)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getTokenTimeout)
defer cancel()

exists, err := capi.db.QueriesRO.ExistTokenHolder(internalCtx, queries.ExistTokenHolderParams{
Expand Down
10 changes: 2 additions & 8 deletions lexer/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lexer

import (
"fmt"
"slices"
"sort"
)

Expand Down Expand Up @@ -62,14 +63,7 @@ func AND(iter *Iteration[[]string]) ([]string, error) {

res := []string{}
for _, a := range dataA {
exist := false
for _, b := range dataB {
if a == b {
exist = true
break
}
}
if exist {
if slices.Contains(dataB, a) {
res = append(res, a)
}
}
Expand Down
12 changes: 5 additions & 7 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package lexer

import "fmt"
import (
"fmt"
"slices"
)

// Lexer struct helps to parse a predicated with a predefined operators,
// generating a iterable struct to eval the it then.
Expand All @@ -16,12 +19,7 @@ func NewLexer(ops []string) *Lexer {
// SupportedOperator method return if the tag provided is a operator supported
// by the current lexer
func (l *Lexer) SupportedOperator(tag string) bool {
for _, op := range l.ops {
if op == tag {
return true
}
}
return false
return slices.Contains(l.ops, tag)
}

// Parse method tokenize the predicate provided a decode its groups levels
Expand Down
10 changes: 2 additions & 8 deletions lexer/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"math/rand"
"slices"
"strings"
)

Expand Down Expand Up @@ -94,14 +95,7 @@ func (t *Token) AllLiterals() []string {
}
res := []string{}
commit := func(i string) {
exists := false
for _, r := range res {
if r == i {
exists = true
break
}
}
if !exists {
if !slices.Contains(res, i) {
res = append(res, i)
}
}
Expand Down

0 comments on commit 1b3e9aa

Please sign in to comment.