MCPcopy Create free account
hub / github.com/daodst/chat / nextTransaction

Method nextTransaction

federationapi/queue/destinationqueue.go:359–454  ·  view source on GitHub ↗

nextTransaction creates a new transaction from the pending event queue and sends it. Returns true if a transaction was sent or false otherwise.

(
	pdus []*queuedPDU,
	edus []*queuedEDU,
)

Source from the content-addressed store, hash-verified

357// queue and sends it. Returns true if a transaction was sent or
358// false otherwise.
359func (oq *destinationQueue) nextTransaction(
360 pdus []*queuedPDU,
361 edus []*queuedEDU,
362) (bool, int, int, error) {
363 // If there's no projected transaction ID then generate one. If
364 // the transaction succeeds then we'll set it back to "" so that
365 // we generate a new one next time. If it fails, we'll preserve
366 // it so that we retry with the same transaction ID.
367 oq.transactionIDMutex.Lock()
368 if oq.transactionID == "" {
369 now := gomatrixserverlib.AsTimestamp(time.Now())
370 oq.transactionID = gomatrixserverlib.TransactionID(fmt.Sprintf("%d-%d", now, oq.statistics.SuccessCount()))
371 }
372 oq.transactionIDMutex.Unlock()
373
374 // Create the transaction.
375 t := gomatrixserverlib.Transaction{
376 PDUs: []json.RawMessage{},
377 EDUs: []gomatrixserverlib.EDU{},
378 }
379 t.Origin = oq.origin
380 t.Destination = oq.destination
381 t.OriginServerTS = gomatrixserverlib.AsTimestamp(time.Now())
382 t.TransactionID = oq.transactionID
383
384 // If we didn't get anything from the database and there are no
385 // pending EDUs then there's nothing to do - stop here.
386 if len(pdus) == 0 && len(edus) == 0 {
387 return false, 0, 0, nil
388 }
389
390 var pduReceipts []*shared.Receipt
391 var eduReceipts []*shared.Receipt
392
393 // Go through PDUs that we retrieved from the database, if any,
394 // and add them into the transaction.
395 for _, pdu := range pdus {
396 if pdu == nil || pdu.pdu == nil {
397 continue
398 }
399 // Append the JSON of the event, since this is a json.RawMessage type in the
400 // gomatrixserverlib.Transaction struct
401 t.PDUs = append(t.PDUs, pdu.pdu.JSON())
402 pduReceipts = append(pduReceipts, pdu.receipt)
403 }
404
405 // Do the same for pending EDUS in the queue.
406 for _, edu := range edus {
407 if edu == nil || edu.edu == nil {
408 continue
409 }
410 t.EDUs = append(t.EDUs, *edu.edu)
411 eduReceipts = append(eduReceipts, edu.receipt)
412 }
413
414 logrus.WithField("server_name", oq.destination).Debugf("Sending transaction %q containing %d PDUs, %d EDUs", t.TransactionID, len(t.PDUs), len(t.EDUs))
415
416 // Try to send the transaction to the destination server.

Callers 1

backgroundSendMethod · 0.95

Calls 7

LockMethod · 0.80
UnlockMethod · 0.80
ContextMethod · 0.80
SendTransactionMethod · 0.65
CleanPDUsMethod · 0.65
CleanEDUsMethod · 0.65
SuccessCountMethod · 0.45

Tested by

no test coverage detected