summaryrefslogtreecommitdiff
path: root/bank/src/main/kotlin/tech/libeufin/bank/db/NotificationWatcher.kt
blob: eb95e2c45a88a57440c056d96d68d9295ec83097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * This file is part of LibEuFin.
 * Copyright (C) 2023 Taler Systems S.A.

 * LibEuFin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3, or
 * (at your option) any later version.

 * LibEuFin is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
 * Public License for more details.

 * You should have received a copy of the GNU Affero General Public
 * License along with LibEuFin; see the file COPYING.  If not, see
 * <http://www.gnu.org/licenses/>
 */

package tech.libeufin.bank.db

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import org.postgresql.ds.PGSimpleDataSource
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import tech.libeufin.bank.*
import tech.libeufin.common.*
import java.util.*
import java.util.concurrent.ConcurrentHashMap

private val logger: Logger = LoggerFactory.getLogger("libeufin-bank-db-watcher")

/** Postgres notification collector and distributor */
internal class NotificationWatcher(private val pgSource: PGSimpleDataSource) {
    // ShareFlow that are manually counted for manual garbage collection
    private class CountedSharedFlow<T>(val flow: MutableSharedFlow<T>, var count: Int)

    // Transaction flows, the keys are the bank account id
    private val bankTxFlows = ConcurrentHashMap<Long, CountedSharedFlow<Long>>()
    private val outgoingTxFlows = ConcurrentHashMap<Long, CountedSharedFlow<Long>>()
    private val incomingTxFlows = ConcurrentHashMap<Long, CountedSharedFlow<Long>>()
    private val revenueTxFlows = ConcurrentHashMap<Long, CountedSharedFlow<Long>>()
    // Withdrawal confirmation flow, the key is the public withdrawal UUID
    private val withdrawalFlow = ConcurrentHashMap<UUID, CountedSharedFlow<WithdrawalStatus>>()

    private val backoff = ExpoBackoffDecorr()

    init {
        // Run notification logic in a separated thread
        kotlin.concurrent.thread(isDaemon = true) { 
            runBlocking {
                while (true) {
                    try {
                        val conn = pgSource.pgConnection()

                        // Listen to all notifications channels
                        conn.execSQLUpdate("LISTEN bank_tx")
                        conn.execSQLUpdate("LISTEN outgoing_tx")
                        conn.execSQLUpdate("LISTEN incoming_tx")
                        conn.execSQLUpdate("LISTEN withdrawal_status")

                        backoff.reset()

                        while (true) {
                            conn.getNotifications(0) // Block until we receive at least one notification
                                .forEach {
                                // Extract information and dispatch
                                when (it.name) {
                                    "bank_tx" -> {
                                        val (debtor, creditor, debitRow, creditRow) = it.parameter.split(' ', limit = 4).map { it.toLong() }
                                        bankTxFlows[debtor]?.run {
                                            flow.emit(debitRow)
                                        }
                                        bankTxFlows[creditor]?.run {
                                            flow.emit(creditRow)
                                        }
                                        revenueTxFlows[creditor]?.run {
                                            flow.emit(creditRow)
                                        }
                                    }
                                    "outgoing_tx" -> {
                                        val (account, merchant, debitRow, creditRow) = it.parameter.split(' ', limit = 4).map { it.toLong() }
                                        outgoingTxFlows[account]?.run {
                                            flow.emit(debitRow)
                                        }
                                    }
                                    "incoming_tx" -> {
                                        val (account, row) = it.parameter.split(' ', limit = 2).map { it.toLong() }
                                        incomingTxFlows[account]?.run {
                                            flow.emit(row)
                                        }
                                    }
                                    "withdrawal_status" -> {
                                        val raw = it.parameter.split(' ', limit = 2)
                                        val uuid = UUID.fromString(raw[0])
                                        val status = WithdrawalStatus.valueOf(raw[1])
                                        withdrawalFlow[uuid]?.run {
                                            flow.emit(status)
                                        }
                                    }
                                }
                            }
                        }
                    } catch (e: Exception) {
                        e.fmtLog(logger)
                        delay(backoff.next())
                    }
                }
            }
        }
    }

    /** Listen to flow from [map] for [key] using [lambda]*/
    private suspend fun <R, K, V> listen(map: ConcurrentHashMap<K, CountedSharedFlow<V>>, key: K, lambda: suspend (Flow<V>) -> R): R {
        // Register listener, create a new flow if missing
        val flow = map.compute(key) { _, v ->
            val tmp = v ?: CountedSharedFlow(MutableSharedFlow(), 0)
            tmp.count++
            tmp
        }!!.flow

        try {
            return lambda(flow)
        } finally {
            // Unregister listener, removing unused flow
            map.compute(key) { _, v ->
                v!!
                v.count--
                if (v.count > 0) v else null
            }
        }
    } 

    /** Listen for new bank transactions for [account] */
    suspend fun <R> listenBank(account: Long, lambda: suspend (Flow<Long>) -> R): R
        = listen(bankTxFlows, account, lambda)
    /** Listen for new taler outgoing transactions from [account] */
    suspend fun <R> listenOutgoing(exchange: Long, lambda: suspend (Flow<Long>) -> R): R
        = listen(outgoingTxFlows, exchange, lambda)
    /** Listen for new taler incoming transactions to [account] */
    suspend fun <R> listenIncoming(exchange: Long, lambda: suspend (Flow<Long>) -> R): R
        = listen(incomingTxFlows, exchange, lambda)
    /** Listen for new taler outgoing transactions to [account] */
    suspend fun <R> listenRevenue(merchant: Long, lambda: suspend (Flow<Long>) -> R): R
        = listen(revenueTxFlows, merchant, lambda)
    /** Listen for new withdrawal confirmations */
    suspend fun <R> listenWithdrawals(withdrawal: UUID, lambda: suspend (Flow<WithdrawalStatus>) -> R): R
        = listen(withdrawalFlow, withdrawal, lambda)
}