startHeartbeat starts heartbeat for read-write transaction. If no reads or DMLs happen within 10 seconds, the rw-transaction is considered idle at Cloud Spanner server. This "SELECT 1" query prevents the transaction from being considered idle. cf. https://godoc.org/cloud.google.com/go/spanner#hdr-I
()
| 379 | // at least one user-initialized SQL query has been executed on the transaction. |
| 380 | // Background: https://github.com/cloudspannerecosystem/spanner-cli/issues/100 |
| 381 | func (s *Session) startHeartbeat() { |
| 382 | interval := time.NewTicker(5 * time.Second) |
| 383 | defer interval.Stop() |
| 384 | |
| 385 | for { |
| 386 | select { |
| 387 | case <-interval.C: |
| 388 | s.tcMutex.Lock() |
| 389 | if s.tc != nil && s.tc.rwTxn != nil && s.tc.sendHeartbeat { |
| 390 | heartbeat(s.tc.rwTxn, s.currentPriority()) |
| 391 | } |
| 392 | s.tcMutex.Unlock() |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | func heartbeat(txn *spanner.ReadWriteStmtBasedTransaction, priority pb.RequestOptions_Priority) error { |
| 398 | ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) |
no test coverage detected