Canonicalize JSON and mention race condition

Signed-off-by: Andrew (anoa) <anoa@openmailbox.org>
This commit is contained in:
Andrew (anoa) 2017-11-12 12:55:16 -08:00
parent 5a09d5123d
commit 8c217e9f6e
No known key found for this signature in database
GPG key ID: 174BEAB009FD176D

View file

@ -17,6 +17,8 @@ package accounts
import ( import (
"context" "context"
"database/sql" "database/sql"
"github.com/matrix-org/gomatrixserverlib"
) )
const filterSchema = ` const filterSchema = `
@ -70,7 +72,7 @@ func (s *filterStatements) prepare(db *sql.DB) (err error) {
func (s *filterStatements) selectFilter( func (s *filterStatements) selectFilter(
ctx context.Context, localpart string, filterID string, ctx context.Context, localpart string, filterID string,
) (filter string, err error) { ) (filter string, err error) {
err = s.selectFilterStmt.QueryRowContext(ctx, localpart, filterID).Scan(&filter) err = s.selectFilterStmt.QueryRowContext(ctx, localpart, filterID).Scan(&filterJSON)
return return
} }
@ -79,13 +81,21 @@ func (s *filterStatements) insertFilter(
) (pos string, err error) { ) (pos string, err error) {
var existingFilter string var existingFilter string
// This can result in a race condition when two clients try to insert the
// same filter and localpart at the same time, however this is not a
// problem as both calls will result in the same filterID
filterJSON, errN := gomatrixserverlib.CanonicalJSON(filter)
if err {
return "", err
}
// Check if filter already exists in the database // Check if filter already exists in the database
err = s.selectFilterByContentStmt.QueryRowContext(ctx, err = s.selectFilterByContentStmt.QueryRowContext(ctx,
localpart, filter).Scan(&existingFilter) localpart, filterJSON).Scan(&existingFilter)
if existingFilter != "" { if existingFilter != "" {
return existingFilter, err return existingFilter, err
} }
err = s.insertFilterStmt.QueryRowContext(ctx, filter, localpart).Scan(&pos) err = s.insertFilterStmt.QueryRowContext(ctx, filterJSON, localpart).Scan(&pos)
return return
} }