Debian Codegraph @

appservice/txnid.go

raw
1 // Copyright (c) 2021 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package appservice
8
9 import "sync"
10
11 type TransactionIDCache struct {
12 array []string
13 arrayPtr int
14 hash map[string]struct{}
15 lock sync.RWMutex
16 }
17
18 func NewTransactionIDCache(size int) *TransactionIDCache {
19 return &TransactionIDCache{
20 array: make([]string, size),
21 hash: make(map[string]struct{}),
22 }
23 }
24
25 func (txnIDC *TransactionIDCache) IsProcessed(txnID string) bool {
26 txnIDC.lock.RLock()
27 _, exists := txnIDC.hash[txnID]
28 txnIDC.lock.RUnlock()
29 return exists
30 }
31
32 func (txnIDC *TransactionIDCache) MarkProcessed(txnID string) {
33 txnIDC.lock.Lock()
34 txnIDC.hash[txnID] = struct{}{}
35 if txnIDC.array[txnIDC.arrayPtr] != "" {
36 for i := 0; i < len(txnIDC.array)/8; i++ {
37 delete(txnIDC.hash, txnIDC.array[txnIDC.arrayPtr+i])
38 txnIDC.array[txnIDC.arrayPtr+i] = ""
39 }
40 }
41 txnIDC.array[txnIDC.arrayPtr] = txnID
42 txnIDC.lock.Unlock()
43 }