diff --git a/engine/action.go b/engine/action.go index f7be99cde..f40e092e7 100644 --- a/engine/action.go +++ b/engine/action.go @@ -355,7 +355,9 @@ func resetCountersAction(ub *Account, sq *StatsQueueTriggered, a *Action, acs Ac if ub == nil { return errors.New("nil user balance") } - ub.UnitCounters.resetCounters(a) + if ub.UnitCounters != nil { + ub.UnitCounters.resetCounters(a) + } return } @@ -404,7 +406,7 @@ func genericReset(ub *Account) error { for k, _ := range ub.BalanceMap { ub.BalanceMap[k] = BalanceChain{&Balance{Value: 0}} } - ub.UnitCounters = make(UnitCounters, 0) + ub.InitCounters() ub.ResetActionTriggers(nil) return nil } diff --git a/engine/storage_map.go b/engine/storage_map.go index da13aec99..76b695484 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -456,18 +456,6 @@ func (ms *MapStorage) GetAccount(key string) (ub *Account, err error) { } func (ms *MapStorage) SetAccount(ub *Account) (err error) { - // never override existing account with an empty one - // UPDATE: if all balances expired and were clean it makes - // sense to write empty balance map - if len(ub.BalanceMap) == 0 { - if ac, err := ms.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { - ac.ActionTriggers = ub.ActionTriggers - ac.UnitCounters = ub.UnitCounters - ac.AllowNegative = ub.AllowNegative - ac.Disabled = ub.Disabled - ub = ac - } - } result, err := ms.ms.Marshal(ub) ms.dict[utils.ACCOUNT_PREFIX+ub.Id] = result return diff --git a/engine/storage_mongo.go b/engine/storage_mongo.go index 3edad9cc9..5450d063d 100644 --- a/engine/storage_mongo.go +++ b/engine/storage_mongo.go @@ -732,18 +732,6 @@ func (ms *MongoStorage) GetAccount(key string) (result *Account, err error) { } func (ms *MongoStorage) SetAccount(acc *Account) error { - // never override existing account with an empty one - // UPDATE: if all balances expired and were cleaned it makes - // sense to write empty balance map - if len(acc.BalanceMap) == 0 { - if ac, err := ms.GetAccount(acc.Id); err == nil && !ac.allBalancesExpired() { - ac.ActionTriggers = acc.ActionTriggers - ac.UnitCounters = acc.UnitCounters - ac.AllowNegative = acc.AllowNegative - ac.Disabled = acc.Disabled - acc = ac - } - } _, err := ms.db.C(colAcc).Upsert(bson.M{"id": acc.Id}, acc) return err } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 72ccbb664..e91f23da5 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -555,18 +555,6 @@ func (rs *RedisStorage) GetAccount(key string) (ub *Account, err error) { } func (rs *RedisStorage) SetAccount(ub *Account) (err error) { - // never override existing account with an empty one - // UPDATE: if all balances expired and were cleaned it makes - // sense to write empty balance map - if len(ub.BalanceMap) == 0 { - if ac, err := rs.GetAccount(ub.Id); err == nil && !ac.allBalancesExpired() { - ac.ActionTriggers = ub.ActionTriggers - ac.UnitCounters = ub.UnitCounters - ac.AllowNegative = ub.AllowNegative - ac.Disabled = ub.Disabled - ub = ac - } - } result, err := rs.ms.Marshal(ub) err = rs.db.Set(utils.ACCOUNT_PREFIX+ub.Id, result) return diff --git a/engine/units_counter.go b/engine/units_counter.go index b07065eed..a25602504 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -31,6 +31,9 @@ type UnitCounters []*UnitCounter func (ucs UnitCounters) addUnits(amount float64, kind string, cc *CallCost, b *Balance) { for _, uc := range ucs { + if uc == nil { // safeguard + continue + } if uc.BalanceType != kind { continue } @@ -53,6 +56,9 @@ func (ucs UnitCounters) addUnits(amount float64, kind string, cc *CallCost, b *B func (ucs UnitCounters) resetCounters(a *Action) { for _, uc := range ucs { + if uc == nil { // safeguard + continue + } if a != nil && a.BalanceType != "" && a.BalanceType != uc.BalanceType { continue }