internal

package
v0.99.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DeviceListChanged = 1
	DeviceListLeft    = 2
)
View Source
const SentryCtxKey = "sliding-sync"

SentryCtxKey is a namespace under which we can report arbitrary key-value pairs to sentry.

View Source
const StateKeyLazy = "$LAZY"

Variables

This section is empty.

Functions

func Assert

func Assert(msg string, expr bool, debugContext ...map[string]interface{})

Assert that the expression is true, similar to assert() in C. If expr is false, print or panic.

If expr is false and SYNCV3_DEBUG=1 then the program panics. If expr is false and SYNCV3_DEBUG is unset or not '1' then the program logs an error along with a field which contains the file/line number of the caller/assertion of Assert. Assert should be used to verify invariants which should never be broken during normal functioning of the program, and shouldn't be used to log a normal error e.g network errors. Developers can make use of this function by setting SYNCV3_DEBUG=1 when running the server, which will fail-fast whenever a programming or logic error occurs. If expr is false and SYNCV3_SENTRY_DSN is configured, an error event is sent to Sentry, including the msg verbatim.

The msg provided should be the expectation of the assert e.g:

Assert("list is not empty", len(list) > 0)

Which then produces:

assertion failed: list is not empty

An optional debugContext map can be provided. If it is present and sentry is configured, it is added as context to the sentry events generated for failed assertions.

func AssertWithContext added in v0.99.2

func AssertWithContext(ctx context.Context, msg string, expr bool)

AssertWithContext is a version of Assert that associates any sentry events with a request context.

func AssociateUserIDWithRequest added in v0.99.16

func AssociateUserIDWithRequest(ctx context.Context, userID, deviceID string) context.Context

add the user ID to this request context. Need to have called RequestContext first.

func CalculateAvatar added in v0.99.5

func CalculateAvatar(metadata *RoomMetadata, isDM bool) string

CalculateAvatar computes the avatar for the room, based on the global room metadata. Assumption: metadata.RemoveHero has been called to remove the user who is syncing from the list of heroes.

func CalculateRoomName

func CalculateRoomName(heroInfo *RoomMetadata, maxNumNamesPerRoom int) (name string, calculated bool)

CalculateRoomName calculates the room name. Returns the name and if the name was actually calculated based on room heroes.

func ConfigureOTLP added in v0.99.7

func ConfigureOTLP(otlpURL, otlpUser, otlpPass, version string) error

func DecorateLogger

func DecorateLogger(ctx context.Context, l *zerolog.Event) *zerolog.Event

func DeviceListChangesArrays

func DeviceListChangesArrays(m map[string]int) (changed, left []string)

func ExtractAccessToken added in v0.99.3

func ExtractAccessToken(req *http.Request) (accessToken string, err error)

func GetBaseURL added in v0.99.13

func GetBaseURL(httpOrUnixStr string) string

func GetSentryHubFromContextOrDefault added in v0.99.2

func GetSentryHubFromContextOrDefault(ctx context.Context) *sentry.Hub

GetSentryHubFromContextOrDefault is a version of sentry.GetHubFromContext which automatically falls back to sentry.CurrentHub if the given context has not been attached a hub.

The various golang sentry integrations automatically attach a hub to contexts that are generated when serving HTTP requests. If that accounts for all your contexts, you have no need for this function; you can use sentry.GetHubFromContext without fear.

The returned pointer is always nonnil.

func IsMembershipChange

func IsMembershipChange(eventJSON gjson.Result) bool

func IsUnixSocket added in v0.99.13

func IsUnixSocket(httpOrUnixStr string) bool

func Keys added in v0.99.12

func Keys[K comparable, V any](m map[K]V) []K

Keys returns a slice containing copies of the keys of the given map, in no particular order.

func Logf added in v0.99.1

func Logf(ctx context.Context, category, format string, args ...interface{})

func NewDataError added in v0.99.6

func NewDataError(msg string, args ...interface{}) error

func ReportPanicsToSentry added in v0.99.3

func ReportPanicsToSentry()

ReportPanicsToSentry checks for panics by calling recover, reports any panic found to sentry, and then reraises the panic. To have tracebacks included in the report, make sure you call panic with something that implements error. (Anything else will be reported as a "message" rather than an "exception" in Sentry; by default, only "exceptions" are reported with tracebacks. See e.g.

   https://github.com/getsentry/sentry-go/blob/eec094e9470dd3855eaf47b025d853bcbc13df68/client.go#L438-L447
for some of the machinery.)

Typically, you want to call this in the form `defer internal.ReportPanicsToSentry()`.

func RequestContext

func RequestContext(ctx context.Context) context.Context

prepare a request context so it can contain syncv3 info

func RequestContextDurations added in v0.99.9

func RequestContextDurations(ctx context.Context) (setup time.Duration, processing time.Duration)

func SetAttributeOnContext added in v0.99.7

func SetAttributeOnContext(ctx context.Context, key TraceKey, val string) context.Context

SetAttributeOnContext sets one of the trace tag keys on the given context, so derived spans will use said tags.

func SetConnBufferInfo added in v0.99.5

func SetConnBufferInfo(ctx context.Context, bufferLen, nextLen, bufferCap int)

func SetRequestContextProcessingDuration added in v0.99.9

func SetRequestContextProcessingDuration(ctx context.Context, processing time.Duration)

func SetRequestContextResponseInfo

func SetRequestContextResponseInfo(
	ctx context.Context, since, next int64, numRooms int, txnID string, numToDeviceEvents, numGlobalAccountData int,
	numChangedDevices, numLeftDevices int, connID string, numLists int, roomSubs, roomUnsubs int,
)

func SetRequestContextSetupDuration added in v0.99.9

func SetRequestContextSetupDuration(ctx context.Context, setup time.Duration)

func ToDeviceListChangesMap

func ToDeviceListChangesMap(changed, left []string) map[string]int

func UnixTransport added in v0.99.13

func UnixTransport(httpOrUnixStr string) *http.Transport

Types

type DataError added in v0.99.6

type DataError struct {
	// contains filtered or unexported fields
}

DataError indicates that this error is caused by invalid data, so retrying this request will not make any difference. This is used by the poller to hint at whether or not we should advance the since token on error.

func (*DataError) Error added in v0.99.6

func (e *DataError) Error() string

type DeviceData

type DeviceData struct {
	// Contains the latest device_one_time_keys_count values.
	// Set whenever this field arrives down the v2 poller, and it replaces what was previously there.
	OTKCounts MapStringInt `json:"otk"`
	// Contains the latest device_unused_fallback_key_types value
	// Set whenever this field arrives down the v2 poller, and it replaces what was previously there.
	// If this is a nil slice this means no change. If this is an empty slice then this means the fallback key was used up.
	FallbackKeyTypes []string `json:"fallback"`

	DeviceLists DeviceLists `json:"dl"`

	// bitset for which device data changes are present. They accumulate until they get swapped over
	// when they get reset
	ChangedBits int `json:"c"`

	UserID   string
	DeviceID string
}

DeviceData contains useful data for this user's device. This list can be expanded without prompting schema changes. These values are upserted into the database and persisted forever.

func (*DeviceData) FallbackKeysChanged added in v0.98.1

func (dd *DeviceData) FallbackKeysChanged() bool

func (*DeviceData) OTKCountChanged added in v0.98.1

func (dd *DeviceData) OTKCountChanged() bool

func (*DeviceData) SetFallbackKeysChanged added in v0.98.1

func (dd *DeviceData) SetFallbackKeysChanged()

func (*DeviceData) SetOTKCountChanged added in v0.98.1

func (dd *DeviceData) SetOTKCountChanged()

type DeviceDataMap

type DeviceDataMap struct {
	Pos int64
	// contains filtered or unexported fields
}

func NewDeviceDataMap

func NewDeviceDataMap(startPos int64, devices []DeviceData) *DeviceDataMap

func (*DeviceDataMap) Get

func (d *DeviceDataMap) Get(userID, deviceID string) *DeviceData

func (*DeviceDataMap) Update

func (d *DeviceDataMap) Update(dd DeviceData) DeviceData

type DeviceLists

type DeviceLists struct {
	// map user_id -> DeviceList enum
	New  MapStringInt `json:"n"`
	Sent MapStringInt `json:"s"`
}

func (DeviceLists) Combine

func (dl DeviceLists) Combine(newer DeviceLists) DeviceLists

type EventMetadata added in v0.99.3

type EventMetadata struct {
	NID       int64
	Timestamp uint64
}

EventMetadata holds timing information about an event, to be used when sorting room lists by recency.

type HandlerError

type HandlerError struct {
	StatusCode int
	Err        error
	ErrCode    string
}

func ExpiredSessionError

func ExpiredSessionError() *HandlerError

func (*HandlerError) Error

func (e *HandlerError) Error() string

func (HandlerError) JSON

func (e HandlerError) JSON() []byte

func (*HandlerError) Unwrap added in v0.99.3

func (e *HandlerError) Unwrap() error

type Hero

type Hero struct {
	ID     string `json:"user_id"`
	Name   string `json:"displayname,omitempty"`
	Avatar string `json:"avatar_url,omitempty"`
}

type MapStringInt added in v0.99.6

type MapStringInt map[string]int

func (MapStringInt) Value added in v0.99.6

func (dl MapStringInt) Value() (driver.Value, error)

Value implements driver.Valuer

type Receipt

type Receipt struct {
	RoomID    string `db:"room_id"`
	EventID   string `db:"event_id"`
	UserID    string `db:"user_id"`
	TS        int64  `db:"ts"`
	ThreadID  string `db:"thread_id"`
	IsPrivate bool
}

type RequiredStateMap

type RequiredStateMap struct {
	// contains filtered or unexported fields
}

func NewRequiredStateMap

func NewRequiredStateMap(eventTypesWithWildcardStateKeys map[string]struct{},
	stateKeysForWildcardEventType []string,
	eventTypeToStateKeys map[string][]string,
	allState, lazyLoading bool) *RequiredStateMap

func (*RequiredStateMap) Empty added in v0.99.0

func (rsm *RequiredStateMap) Empty() bool

func (*RequiredStateMap) Include

func (rsm *RequiredStateMap) Include(evType, stateKey string) bool

func (*RequiredStateMap) IsLazyLoading

func (rsm *RequiredStateMap) IsLazyLoading() bool

func (*RequiredStateMap) QueryStateMap

func (rsm *RequiredStateMap) QueryStateMap() map[string][]string

work out what to ask the storage layer: if we have wildcard event types we need to pull all room state and cannot only pull out certain event types. If we have wildcard state keys we need to use an empty list for state keys.

type RoomMetadata

type RoomMetadata struct {
	RoomID         string
	Heroes         []Hero
	NameEvent      string // the content of m.room.name, NOT the calculated name
	AvatarEvent    string // the content of m.room.avatar, NOT the resolved avatar
	CanonicalAlias string
	JoinCount      int
	InviteCount    int
	// LastMessageTimestamp is the origin_server_ts of the event most recently seen in
	// this room. Because events arrive at the upstream homeserver out-of-order (and
	// because origin_server_ts is an untrusted event field), this timestamp can
	// _decrease_ as new events come in.
	LastMessageTimestamp uint64
	// LatestEventsByType tracks timing information for the latest event in the room,
	// grouped by event type.
	LatestEventsByType map[string]EventMetadata
	Encrypted          bool
	PredecessorRoomID  *string
	UpgradedRoomID     *string
	RoomType           *string
	// if this room is a space, which rooms are m.space.child state events. This is the same for all users hence is global.
	ChildSpaceRooms map[string]struct{}
	// The latest m.typing ephemeral event for this room.
	TypingEvent json.RawMessage
}

RoomMetadata holds room-scoped data. TODO: This is a lie: we sometimes remove a user U from the list of heroes when calculating the sync response for that user U. Grep for `RemoveHero`.

It is primarily used in two places:

  • in the caches.GlobalCache, to hold the latest version of data that is consistent between all users in the room; and
  • in the sync3.RoomConnMetadata struct, to hold the version of data last seen by a given user sync connection.

Roughly speaking, the sync3.RoomConnMetadata is constantly catching up with changes in the caches.GlobalCache.

func NewRoomMetadata added in v0.99.3

func NewRoomMetadata(roomID string) *RoomMetadata

func (*RoomMetadata) DeepCopy added in v0.99.12

func (m *RoomMetadata) DeepCopy() *RoomMetadata

DeepCopy returns a version of the current RoomMetadata whose Heroes+LatestEventsByType fields are brand-new copies. The return value's Heroes field can be safely modified by the caller, but it is NOT safe for the caller to modify any other fields.

func (*RoomMetadata) IsSpace

func (m *RoomMetadata) IsSpace() bool

func (*RoomMetadata) RemoveHero

func (m *RoomMetadata) RemoveHero(userID string)

func (*RoomMetadata) SameInviteCount

func (m *RoomMetadata) SameInviteCount(other *RoomMetadata) bool

func (*RoomMetadata) SameJoinCount

func (m *RoomMetadata) SameJoinCount(other *RoomMetadata) bool

func (*RoomMetadata) SameRoomName

func (m *RoomMetadata) SameRoomName(other *RoomMetadata) bool

SameRoomName checks if the fields relevant for room names have changed between the two metadatas. Returns true if there are no changes.

type RuntimeTraceOTLPSpan added in v0.99.1

type RuntimeTraceOTLPSpan struct {
	// contains filtered or unexported fields
}

combined runtime/trace and OTLP span

func StartSpan added in v0.99.1

func StartSpan(ctx context.Context, name string) (newCtx context.Context, span *RuntimeTraceOTLPSpan)

func (*RuntimeTraceOTLPSpan) End added in v0.99.1

func (s *RuntimeTraceOTLPSpan) End()

type Task added in v0.99.1

type Task struct {
	// contains filtered or unexported fields
}

func StartTask added in v0.99.1

func StartTask(ctx context.Context, name string) (context.Context, *Task)

func (*Task) End added in v0.99.1

func (s *Task) End()

type TraceKey added in v0.99.1

type TraceKey string
var (
	OTLPTagDeviceID TraceKey = "device_id"
	OTLPTagUserID   TraceKey = "user_id"
	OTLPTagConnID   TraceKey = "conn_id"
	OTLPTagTxnID    TraceKey = "txn_id"
)

type UserDeviceKey

type UserDeviceKey struct {
	UserID   string
	DeviceID string
}

type WorkerPool added in v0.99.4

type WorkerPool struct {
	N int
	// contains filtered or unexported fields
}

func NewWorkerPool added in v0.99.4

func NewWorkerPool(n int) *WorkerPool

Create a new worker pool of size N. Up to N work can be done concurrently. The size of N depends on the expected frequency of work and contention for shared resources. Large values of N allow more frequent work at the cost of more contention for shared resources like cpu, memory and fds. Small values of N allow less frequent work but control the amount of shared resource contention. Ideally this value will be derived from whatever shared resource constraints you are hitting up against, rather than set to a fixed value. For example, if you have a database connection limit of 100, then setting N to some fraction of the limit is preferred to setting this to an arbitrary number < 100. If more than N work is requested, eventually WorkerPool.Queue will block until some work is done.

The larger N is, the larger the up front memory costs are due to the implementation of WorkerPool.

func (*WorkerPool) Queue added in v0.99.4

func (wp *WorkerPool) Queue(fn func())

Queue some work on the pool. May or may not block until some work is processed.

func (*WorkerPool) Start added in v0.99.4

func (wp *WorkerPool) Start()

Start the workers. Only call this once.

func (*WorkerPool) Stop added in v0.99.4

func (wp *WorkerPool) Stop()

Stop the worker pool. Only really useful for tests as a worker pool should be started once and persist for the lifetime of the process, else it causes needless goroutine churn. Only call this once.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL