This example demonstrates how batching can be used to group similar concurrent database updates into a single query. The UpdateUserTimestamp function is used to update the last_active_at column in the users table. Updates are not executed immediately, but are rather queued and then sent to the datab
()
| 112 | // |
| 113 | // For simplicity, this example does not have retries, error handling and synchronization |
| 114 | func Example_batchingRealTime() { |
| 115 | // Start the background worker that processes the updates |
| 116 | go updateUserTimestampWorker() |
| 117 | |
| 118 | // Do some updates. They'll be automatically grouped into |
| 119 | // batches: [1,2,3,4,5], [6,7], [8] |
| 120 | UpdateUserTimestamp(1) |
| 121 | UpdateUserTimestamp(2) |
| 122 | UpdateUserTimestamp(3) |
| 123 | UpdateUserTimestamp(4) |
| 124 | UpdateUserTimestamp(5) |
| 125 | UpdateUserTimestamp(6) |
| 126 | UpdateUserTimestamp(7) |
| 127 | time.Sleep(500 * time.Millisecond) // simulate sparse updates |
| 128 | UpdateUserTimestamp(8) |
| 129 | |
| 130 | // Wait for the updates to be processed |
| 131 | // In real-world application, different synchronization mechanisms would be used. |
| 132 | time.Sleep(1 * time.Second) |
| 133 | } |
| 134 | |
| 135 | // This is the queue of user IDs to update. |
| 136 | var userIDsToUpdate = make(chan int) |
nothing calls this directly
no test coverage detected