Bank: Progress update - DB, decimals and benchmarks
Over the past week I’ve done backend work on the bank. These aren’t sexy changes, but definitely much needed.
DB Connections
When I first wrote the database connections, I wasn’t sure how they worked exactly. I assumed that the database connections were closed on function exit, and that only queries needed to be explicitly closed. I was half right.
Go uses a DB connection pool which is meant to be long lived and shared between functions in the application. This DB pool manages the amount of connections used by queries to the database. All queries must be closed, but the DB pool is only ever opened once and is closed on application exit.
I found this error while testing the new mobile app and receiving the error Error with SQL: too many connections
. After doing some digging, I moved all connections to the global Config variable. I did this for both MySQL and Redis.
After finding this error, I looked into ways to help prevent it in future and landed on using benchmarks.
Benchmarks
I’ve written loads of tests for the application, and added benchmarks to these tests. Again, using Go, this is exceptionally easy. An example benchmark for creating and deleting user accounts looks as follows:
func BenchmarkDoCreateAccount(b *testing.B) {
for n := 0; n < b.N; n++ {
accountDetail := AccountDetails{
"",
"",
"User,Test",
decimal.NewFromFloat(0.),
decimal.NewFromFloat(0.),
decimal.NewFromFloat(0.),
0,
}
ti := time.Now()
sqlTime := int32(ti.Unix())
_ = doCreateAccount(sqlTime, &accountDetail)
_ = doDeleteAccount(&accountDetail)
}
}
Errors are ignored as they are handled by the tests themselves.
Decimals
Throughout the application I was using float64
for working with the transactional data. This was an oversight as floats are known to cause issues when used in finance. As Go does not have native support for decimals, I ended up using a decimals package.
All native float
arithmetic was changed to use the functions of the decimal package.
Travis CI
I also started integrating continuous integration into the project. I’m busy fixing some of the more nuanced errors, but builds should be good to go within a week.
Conclusion
Development is coming along well. As far as next steps go, I’m starting to look at:
- ISO standards
- Prepaid card integration
- Functional testing
- RethinkDB
Follow along on Github.