2017-05-17 16:38:24 +02:00
|
|
|
// Copyright 2017 Vector Creations 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 sync
|
|
|
|
|
|
|
|
import (
|
2017-10-26 12:34:54 +02:00
|
|
|
"context"
|
|
|
|
"runtime"
|
2017-05-17 16:38:24 +02:00
|
|
|
"sync"
|
2017-10-26 12:34:54 +02:00
|
|
|
"time"
|
2017-05-17 16:38:24 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2017-10-26 12:34:54 +02:00
|
|
|
"github.com/matrix-org/util"
|
2017-05-17 16:38:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserStream represents a communication mechanism between the /sync request goroutine
|
2017-10-26 12:34:54 +02:00
|
|
|
// and the underlying sync server goroutines.
|
|
|
|
// Goroutines can get a UserStreamListener to wait for updates, and can Broadcast()
|
|
|
|
// updates.
|
2017-05-17 16:38:24 +02:00
|
|
|
type UserStream struct {
|
|
|
|
UserID string
|
2017-10-26 12:34:54 +02:00
|
|
|
// The lock that protects changes to this struct
|
|
|
|
lock sync.Mutex
|
|
|
|
// Closed when there is an update.
|
|
|
|
signalChannel chan struct{}
|
2019-07-12 16:59:53 +02:00
|
|
|
// The last sync position that there may have been an update for the user
|
|
|
|
pos types.SyncPosition
|
2017-10-26 12:34:54 +02:00
|
|
|
// The last time when we had some listeners waiting
|
|
|
|
timeOfLastChannel time.Time
|
|
|
|
// The number of listeners waiting
|
|
|
|
numWaiting uint
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserStreamListener allows a sync request to wait for updates for a user.
|
|
|
|
type UserStreamListener struct {
|
|
|
|
userStream *UserStream
|
|
|
|
|
|
|
|
// Whether the stream has been closed
|
|
|
|
hasClosed bool
|
2017-05-17 16:38:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserStream creates a new user stream
|
2019-07-12 16:59:53 +02:00
|
|
|
func NewUserStream(userID string, currPos types.SyncPosition) *UserStream {
|
2017-05-17 16:38:24 +02:00
|
|
|
return &UserStream{
|
2017-10-26 12:34:54 +02:00
|
|
|
UserID: userID,
|
|
|
|
timeOfLastChannel: time.Now(),
|
|
|
|
pos: currPos,
|
|
|
|
signalChannel: make(chan struct{}),
|
2017-05-17 16:38:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-26 12:34:54 +02:00
|
|
|
// GetListener returns UserStreamListener that a sync request can use to wait
|
|
|
|
// for new updates with.
|
|
|
|
// UserStreamListener must be closed
|
|
|
|
func (s *UserStream) GetListener(ctx context.Context) UserStreamListener {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
s.numWaiting++ // We decrement when UserStreamListener is closed
|
|
|
|
|
|
|
|
listener := UserStreamListener{
|
|
|
|
userStream: s,
|
2017-05-17 16:38:24 +02:00
|
|
|
}
|
2017-10-26 12:34:54 +02:00
|
|
|
|
|
|
|
// Lets be a bit paranoid here and check that Close() is being called
|
|
|
|
runtime.SetFinalizer(&listener, func(l *UserStreamListener) {
|
|
|
|
if !l.hasClosed {
|
|
|
|
util.GetLogger(ctx).Warn("Didn't call Close on UserStreamListener")
|
|
|
|
l.Close()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return listener
|
2017-05-17 16:38:24 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 16:59:53 +02:00
|
|
|
// Broadcast a new sync position for this user.
|
|
|
|
func (s *UserStream) Broadcast(pos types.SyncPosition) {
|
2017-10-26 12:34:54 +02:00
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
2017-05-17 16:38:24 +02:00
|
|
|
s.pos = pos
|
2017-10-26 12:34:54 +02:00
|
|
|
|
|
|
|
close(s.signalChannel)
|
|
|
|
|
|
|
|
s.signalChannel = make(chan struct{})
|
2017-05-17 16:38:24 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 12:34:54 +02:00
|
|
|
// NumWaiting returns the number of goroutines waiting for waiting for updates.
|
|
|
|
// Used for metrics and testing.
|
|
|
|
func (s *UserStream) NumWaiting() uint {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
2017-05-17 16:38:24 +02:00
|
|
|
return s.numWaiting
|
|
|
|
}
|
2017-10-26 12:34:54 +02:00
|
|
|
|
|
|
|
// TimeOfLastNonEmpty returns the last time that the number of waiting listeners
|
|
|
|
// was non-empty, may be time.Now() if number of waiting listeners is currently
|
|
|
|
// non-empty.
|
|
|
|
func (s *UserStream) TimeOfLastNonEmpty() time.Time {
|
|
|
|
s.lock.Lock()
|
|
|
|
defer s.lock.Unlock()
|
|
|
|
|
|
|
|
if s.numWaiting > 0 {
|
|
|
|
return time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.timeOfLastChannel
|
|
|
|
}
|
|
|
|
|
2019-07-12 16:59:53 +02:00
|
|
|
// GetStreamPosition returns last sync position which the UserStream was
|
2017-10-26 12:34:54 +02:00
|
|
|
// notified about
|
2019-07-12 16:59:53 +02:00
|
|
|
func (s *UserStreamListener) GetSyncPosition() types.SyncPosition {
|
2017-10-26 12:34:54 +02:00
|
|
|
s.userStream.lock.Lock()
|
|
|
|
defer s.userStream.lock.Unlock()
|
|
|
|
|
|
|
|
return s.userStream.pos
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotifyChannel returns a channel that is closed when there may be an
|
|
|
|
// update for the user.
|
|
|
|
// sincePos specifies from which point we want to be notified about. If there
|
|
|
|
// has already been an update after sincePos we'll return a closed channel
|
|
|
|
// immediately.
|
2019-07-12 16:59:53 +02:00
|
|
|
func (s *UserStreamListener) GetNotifyChannel(sincePos types.SyncPosition) <-chan struct{} {
|
2017-10-26 12:34:54 +02:00
|
|
|
s.userStream.lock.Lock()
|
|
|
|
defer s.userStream.lock.Unlock()
|
|
|
|
|
2019-07-12 16:59:53 +02:00
|
|
|
if s.userStream.pos.IsAfter(sincePos) {
|
2017-10-26 12:34:54 +02:00
|
|
|
// If the listener is behind, i.e. missed a potential update, then we
|
|
|
|
// want them to wake up immediately. We do this by returning a new
|
|
|
|
// closed stream, which returns immediately when selected.
|
|
|
|
closedChannel := make(chan struct{})
|
|
|
|
close(closedChannel)
|
|
|
|
return closedChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.userStream.signalChannel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close cleans up resources used
|
|
|
|
func (s *UserStreamListener) Close() {
|
|
|
|
s.userStream.lock.Lock()
|
|
|
|
defer s.userStream.lock.Unlock()
|
|
|
|
|
|
|
|
if !s.hasClosed {
|
|
|
|
s.userStream.numWaiting--
|
|
|
|
s.userStream.timeOfLastChannel = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.hasClosed = true
|
|
|
|
}
|