| 1 | /* |
| 2 | Package lmdbpool provides a TxnPool type that allows lmdb.Readonly transactions |
| 3 | to safely be reused by other goroutines when the goroutine that created the |
| 4 | transaction no longer has a use for it. The TxnPool type has benefits that |
| 5 | would be absent in a naive use of sync.Pool with lmdb.Txn types. |
| 6 | |
| 7 | Naively reusing lmdb.Readonly transactions can cause updates to continually |
| 8 | allocate more pages for the database instead of reusing stale pages. The |
| 9 | TxnPool type tracks transaction ids to make sure that lmdb.Readonly |
| 10 | transactions are not reused when they are known to be holding stale pages which |
| 11 | could be reclaimed by LMDB. |
| 12 | |
| 13 | A general downside of pooling lmdb.Readonly transactions using a sync.Pool in |
| 14 | applications with a very high rate of transacions is that the number of readers |
| 15 | in an environment can be significantly higher than the number of goroutines |
| 16 | actively trying to read from that environment. Because of this it is possible |
| 17 | that applications may need to increase the maximum number of readers allowed in |
| 18 | the environment at initialization time. |
| 19 | |
| 20 | err := env.SetMaxReaders(maxReaders) |
| 21 | |
| 22 | In a naive pooling implementation an application compiled with the -race flag |
| 23 | may require an extremely large number of open readers. The TxnPool type |
| 24 | attempts to keep the value required for Env.SetMaxReaders as low as possible in |
| 25 | the presence of -race but there is a limited amount that can be done for a |
| 26 | concurrent workload with a rapid enough rate of transactions. |
| 27 | */ |
| 28 | package lmdbpool |