additional faffing around with streaming

This commit is contained in:
tsmethurst 2021-06-17 22:51:50 +02:00 committed by tsmethurst
commit e87dec807e
9 changed files with 171 additions and 28 deletions

View file

@ -0,0 +1,108 @@
package streaming
import (
"errors"
"fmt"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
)
func (p *processor) OpenStreamForAccount(conn *websocket.Conn, account *gtsmodel.Account, streamType string) gtserror.WithCode {
l := p.log.WithFields(logrus.Fields{
"func": "OpenStreamForAccount",
"account": account.ID,
"streamType": streamType,
})
l.Debug("received open stream request")
streamID, err := id.NewRandomULID()
if err != nil {
return gtserror.NewErrorInternalError(fmt.Errorf("error generating stream id: %s", err))
}
thisStream := &stream{
streamID: streamID,
streamType: streamType,
conn: conn,
}
v, ok := p.streamMap.Load(account.ID)
if !ok || v == nil {
// there is no entry in the streamMap for this account yet, so make one and store it
streams := &streamsForAccount{
s: []*stream{
thisStream,
},
}
p.streamMap.Store(account.ID, streams)
} else {
// there is an entry in the streamMap for this account
// parse the interface as a streamsForAccount
streams, ok := v.(*streamsForAccount)
if !ok {
return gtserror.NewErrorInternalError(errors.New("stream map error"))
}
// append this stream to it
streams.Lock()
streams.s = append(streams.s, thisStream)
streams.Unlock()
}
// set the close handler to remove the given stream from the stream map so that messages stop getting put into it
conn.SetCloseHandler(func(code int, text string) error {
l.Debug("closing stream")
v, ok := p.streamMap.Load(account.ID)
if !ok || v == nil {
// the map doesn't contain an entry for the account anyway, so we can just return
// this probably should never happen but let's check anyway
return nil
}
// parse the interface as a streamsForAccount
streams, ok := v.(*streamsForAccount)
if !ok {
return gtserror.NewErrorInternalError(errors.New("stream map error"))
}
// remove thisStream from the slice of streams stored in streamsForAccount
streams.Lock()
newStreamSlice := []*stream{}
for _, s := range streams.s {
if s.streamID != thisStream.streamID {
newStreamSlice = append(newStreamSlice, s)
}
}
streams.s = newStreamSlice
streams.Unlock()
l.Debug("stream closed")
return nil
})
defer conn.Close()
t := time.NewTicker(60 * time.Second)
for range t.C {
if err := conn.WriteMessage(websocket.PingMessage, nil); err != nil {
return gtserror.NewErrorInternalError(err)
}
}
return nil
}
type streamsForAccount struct {
s []*stream
sync.Mutex
}
type stream struct {
streamID string
streamType string
conn *websocket.Conn
}

View file

@ -1,22 +0,0 @@
package streaming
import (
"github.com/gorilla/websocket"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
func (p *processor) StreamForAccount(c *websocket.Conn, account *gtsmodel.Account, streamType string) gtserror.WithCode {
v, loaded := p.streamMap.LoadOrStore(account.ID, sync.Slice)
if loaded {
}
return nil
}
type streams struct {
accountID string
}

View file

@ -12,13 +12,15 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/oauth"
"github.com/superseriousbusiness/gotosocial/internal/typeutils"
"github.com/superseriousbusiness/gotosocial/internal/visibility"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
)
// Processor wraps a bunch of functions for processing streaming.
type Processor interface {
// AuthorizeStreamingRequest returns an oauth2 token info in response to an access token query from the streaming API
AuthorizeStreamingRequest(accessToken string) (*gtsmodel.Account, error)
StreamForAccount(c *websocket.Conn, account *gtsmodel.Account, streamType string) gtserror.WithCode
OpenStreamForAccount(c *websocket.Conn, account *gtsmodel.Account, streamType string) gtserror.WithCode
StreamStatusForAccount(s *apimodel.Status, account *gtsmodel.Account) error
}
type processor struct {

View file

@ -0,0 +1,37 @@
package streaming
import (
"errors"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
func (p *processor) StreamStatusForAccount(s *apimodel.Status, account *gtsmodel.Account) error {
l := p.log.WithFields(logrus.Fields{
"func": "StreamStatusForAccount",
"account": account.ID,
})
v, ok := p.streamMap.Load(account.ID)
if !ok {
// no open connections so nothing to stream
return nil
}
streams, ok := v.(*streamsForAccount)
if !ok {
return errors.New("stream map error")
}
streams.Lock()
defer streams.Unlock()
for _, stream := range streams.s {
l.Debugf("streaming status to stream id %s", stream.streamID)
if err := stream.conn.WriteJSON(s); err != nil {
return err
}
}
return nil
}