This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 676
Implement transactions cache to ensure idempotency of sendEvents #419
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/github.com/matrix-org/dendrite/clientapi/transactions/transactions.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2018 New Vector Ltd | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package transactions | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
"github.com/matrix-org/util" | ||
) | ||
|
||
// CleanupPeriod represents time in nanoseconds after which cacheCleanService runs. | ||
const CleanupPeriod time.Duration = 30 * time.Minute | ||
|
||
type response struct { | ||
cache *util.JSONResponse | ||
cacheTime time.Time | ||
} | ||
|
||
// Cache represents a temporary store for responses. | ||
// This is used to ensure idempotency of requests. | ||
type Cache struct { | ||
sync.RWMutex | ||
txns map[string]response | ||
} | ||
|
||
// CreateCache creates and returns a initialized Cache object. | ||
// This cache is automatically cleaned every `CleanupPeriod`. | ||
func CreateCache() *Cache { | ||
t := Cache{txns: make(map[string]response)} | ||
|
||
// Start cleanup service as the Cache is created | ||
go cacheCleanService(&t) | ||
|
||
return &t | ||
} | ||
|
||
// FetchTransaction looks up response for txnID in Cache. | ||
// Returns a JSON response if txnID is found, which can be sent to client, | ||
// else returns error. | ||
func (t *Cache) FetchTransaction(txnID string) (*util.JSONResponse, error) { | ||
t.RLock() | ||
res, ok := t.txns[txnID] | ||
t.RUnlock() | ||
|
||
if ok { | ||
return res.cache, nil | ||
} | ||
|
||
return nil, errors.New("TxnID not present") | ||
} | ||
|
||
// AddTransaction adds a response for txnID in Cache for later access. | ||
func (t *Cache) AddTransaction(txnID string, res *util.JSONResponse) { | ||
t.Lock() | ||
defer t.Unlock() | ||
t.txns[txnID] = response{cache: res, cacheTime: time.Now()} | ||
} | ||
|
||
// cacheCleanService is responsible for cleaning up transactions older than 30 min. | ||
// It guarantees that a transaction will be present in cache for at least 30 min & at most 60 min. | ||
func cacheCleanService(t *Cache) { | ||
for { | ||
time.Sleep(CleanupPeriod) | ||
go clean(t) | ||
} | ||
} | ||
|
||
func clean(t *Cache) { | ||
expire := time.Now().Add(-CleanupPeriod) | ||
for key := range t.txns { | ||
t.Lock() | ||
if t.txns[key].cacheTime.Before(expire) { | ||
delete(t.txns, key) | ||
} | ||
t.Unlock() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You made sure that there is a correlation to txnID so this should work but as you get a pointer of transactionsCache so I would add a null guard before accessing it.