Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
6833098
fix(streaming): close connection send queues on disconnect
juicycleff Nov 16, 2026
6cc3cdd
fix(router): answer WebSocket pings under the write lock
juicycleff Nov 16, 2026
bf9406b
fix(router): cap the size of one WebTransport JSON value
juicycleff Nov 16, 2026
2bd8f2f
fix(streaming): authorize channel subscriptions
juicycleff Nov 16, 2026
22032c8
fix(streaming): let WebSocket clients join rooms
juicycleff Nov 16, 2026
ea05179
fix(streaming): emit SSE event ids for messages sent through the queue
juicycleff Nov 16, 2026
94515c4
perf(streaming): encode a broadcast once instead of per recipient
juicycleff Nov 16, 2026
1ddf934
perf(streaming): deliver relayed room messages through the room index
juicycleff Nov 16, 2026
7caed41
fix(streaming): keep Redis calls out of the manager lock on connect a…
juicycleff Nov 16, 2026
6fca864
fix(streaming): rate limit anonymous connections
juicycleff Nov 16, 2026
a82c138
fix(streaming): mark users offline only when their last connection cl…
juicycleff Nov 16, 2026
a5d299a
fix(streaming): make room bans work on the local backend
juicycleff Nov 16, 2026
6ca1364
feat(streaming): implement room mutes on the Redis backend
juicycleff Nov 16, 2026
40c9f4d
fix(streaming): evict banned users on every node
juicycleff Nov 16, 2026
99153e6
fix(streaming): deliver NATS global messages to every node
juicycleff Nov 16, 2026
262b03b
Merge remote-tracking branch 'origin/main' into fix/streaming
juicycleff Nov 18, 2026
0efd258
build(deps): update modules that build streaming from the local checkout
juicycleff Nov 18, 2026
b920aa7
test(streaming): use t.Context in the channel authorization test
juicycleff Nov 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/webtransport/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ require (
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/serf v0.10.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.19.1 // indirect
github.com/klauspost/compress v1.20.1 // indirect
github.com/leodido/go-urn v1.5.0 // indirect
github.com/mattn/go-colorable v0.1.15 // indirect
github.com/mattn/go-isatty v0.0.24 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nats-io/nats.go v1.47.0 // indirect
github.com/nats-io/nkeys v0.4.11 // indirect
github.com/nats-io/nats.go v1.53.1 // indirect
github.com/nats-io/nkeys v0.4.16 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/prometheus/client_golang v1.24.1 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
Expand Down Expand Up @@ -114,7 +114,7 @@ require (
golang.org/x/sys v0.48.0 // indirect
golang.org/x/term v0.46.0 // indirect
golang.org/x/text v0.42.0 // indirect
golang.org/x/time v0.15.0 // indirect
golang.org/x/time v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20260803160001-6ac0973c030d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20260803160001-6ac0973c030d // indirect
google.golang.org/grpc v1.83.2 // indirect
Expand Down
26 changes: 18 additions & 8 deletions examples/webtransport/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
github.com/alicebob/miniredis/v2 v2.35.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
github.com/antithesishq/antithesis-sdk-go v0.8.0-default-no-op h1:1BOWQJweNyvZMlpAHXGLiZQn9S+QXGcz3xh94lC0w6E=
github.com/antithesishq/antithesis-sdk-go v0.8.0-default-no-op/go.mod h1:FQyySiasQQM8735Ddel3MRojmy4dA1IqCeyJ5jmPMbI=
github.com/armon/go-metrics v0.4.1 h1:hR91U9KYmb6bLBYLQjyM+3j+rcd/UhE+G78SFnF8gJA=
github.com/armon/go-metrics v0.4.1/go.mod h1:E6amYzXo6aW1tqzoZGT755KkbgrJsSdpwZ+3JqfkOG4=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -119,6 +121,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/go-tpm v0.9.8 h1:slArAR9Ft+1ybZu0lBwpSmpwhRXaa85hWtMinMyRAWo=
github.com/google/go-tpm v0.9.8/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -171,8 +175,8 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/klauspost/compress v1.19.1 h1:VsB4HPswih7mmZ8WleSFQ75c/Ui1M4trX5oAsJnhSlk=
github.com/klauspost/compress v1.19.1/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/compress v1.20.1 h1:T7kKElXUMXrUJ2E9QhQhxFtcK5rPyLdsGZvdbLMPdiQ=
github.com/klauspost/compress v1.20.1/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand All @@ -199,6 +203,8 @@ github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
github.com/minio/highwayhash v1.0.4 h1:asJizugGgchQod2ja9NJlGOWq4s7KsAWr5XUc9Clgl4=
github.com/minio/highwayhash v1.0.4/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -212,10 +218,14 @@ github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee/go.mod h1:yWu
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nats-io/nats.go v1.47.0 h1:YQdADw6J/UfGUd2Oy6tn4Hq6YHxCaJrVKayxxFqYrgM=
github.com/nats-io/nats.go v1.47.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0=
github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE=
github.com/nats-io/jwt/v2 v2.8.2 h1:XXRgB60MSTnqsRwejQurVDs/hcv2dkt+86GjI+I/bMc=
github.com/nats-io/jwt/v2 v2.8.2/go.mod h1:Ag/56sq9OblL4JgdYufDd16Egb17Kr/8WwwuO/forVc=
github.com/nats-io/nats-server/v2 v2.14.8 h1:EWd82pDFZ05U3iMS68LRgbNzgi4JB5Er93JpWHA5ng8=
github.com/nats-io/nats-server/v2 v2.14.8/go.mod h1:8lHkzklmT09g8/Vb3I6uQMtuGjDQ3e9DRHb5Ietg7Z8=
github.com/nats-io/nats.go v1.53.1 h1:Otsq3uLc/kLdjmkNHkXH0jBqwUquwdKFoe3fq6/3/Xo=
github.com/nats-io/nats.go v1.53.1/go.mod h1:26HypzazeOkyO3/mqd1zZd53STJN0EjCYF9Uy2ZOBno=
github.com/nats-io/nkeys v0.4.16 h1:rd5oAuLOb8mnAycB0xleuEBNS1pVVnN0fv/FF34Eypg=
github.com/nats-io/nkeys v0.4.16/go.mod h1:llLgWoI0o4z/Q57q2R1kHfmocyhGV6VG/U18Glg1Afs=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY=
Expand Down Expand Up @@ -370,8 +380,8 @@ golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
golang.org/x/time v0.16.0 h1:vMb6ptszcQMkcwiRTAuNNU50gom6++Q/6gY2hDM6VDE=
golang.org/x/time v0.16.0/go.mod h1:rVKOqvZeKvrDKTQiAHJ7wmwP0RzleSphoEA9RcdLA0s=
golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
59 changes: 59 additions & 0 deletions extensions/streaming/auth/channel_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package auth

import (
"context"

"github.com/xraph/forge/errors"
streaming "github.com/xraph/forge/extensions/streaming/internal"
)

// ChannelAuthorizerFunc adapts a function to ChannelAuthorizer.
type ChannelAuthorizerFunc func(ctx context.Context, userID, channelID string) (bool, error)

// CanSubscribe calls f.
func (f ChannelAuthorizerFunc) CanSubscribe(ctx context.Context, userID, channelID string) (bool, error) {
return f(ctx, userID, channelID)
}

// channelAuthorizer is the default ChannelAuthorizer.
type channelAuthorizer struct {
channelStore streaming.ChannelStore
}

// NewChannelAuthorizer returns the default channel policy: anyone the manager
// lets through may subscribe to a public channel, and nobody may subscribe to
// a private one.
//
// A channel has no member list the store could check, so the default policy
// cannot tell who belongs to a private channel and refuses everyone. An
// application that uses private channels supplies its own ChannelAuthorizer,
// which knows its users, for example with ChannelAuthorizerFunc.
//
// A channel the store does not know is left to the store to refuse, as before:
// it has no private flag to leak through.
func NewChannelAuthorizer(channelStore streaming.ChannelStore) ChannelAuthorizer {
return &channelAuthorizer{channelStore: channelStore}
}

// CanSubscribe implements ChannelAuthorizer.
func (ca *channelAuthorizer) CanSubscribe(ctx context.Context, userID, channelID string) (bool, error) {
channel, err := ca.channelStore.Get(ctx, channelID)
if errors.Is(err, streaming.ErrChannelNotFound) {
return true, nil
}

if err != nil {
return false, err
}

return !IsPrivateChannel(channel), nil
}

// IsPrivateChannel reports whether a channel was created with
// ChannelOptions.Private. The Channel interface predates the flag, so it is
// read through an optional IsPrivate method; a channel without one is public.
func IsPrivateChannel(channel streaming.Channel) bool {
p, ok := channel.(interface{ IsPrivate() bool })

return ok && p.IsPrivate()
}
46 changes: 46 additions & 0 deletions extensions/streaming/auth/channel_auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package auth

import (
"context"
"testing"

"github.com/xraph/forge/extensions/streaming/backends/local"
streaming "github.com/xraph/forge/extensions/streaming/internal"
)

func TestChannelAuthorizer_DefaultPolicy(t *testing.T) {
ctx := context.Background()
store := local.NewChannelStore()

for _, opts := range []streaming.ChannelOptions{
{ID: "news", Name: "news"},
{ID: "secret", Name: "secret", Private: true},
} {
if err := store.Create(ctx, local.NewLocalChannel(opts)); err != nil {
t.Fatal(err)
}
}

ca := NewChannelAuthorizer(store)

cases := []struct {
channel string
want bool
}{
{"news", true},
{"secret", false},
// Unknown channels are left to the store, which refuses them itself.
{"missing", true},
}

for _, tc := range cases {
got, err := ca.CanSubscribe(ctx, "alice", tc.channel)
if err != nil {
t.Fatalf("%s: %v", tc.channel, err)
}

if got != tc.want {
t.Errorf("%s: CanSubscribe = %v, want %v", tc.channel, got, tc.want)
}
}
}
12 changes: 12 additions & 0 deletions extensions/streaming/auth/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ type RoomAuthorizer interface {
GetUserRole(ctx context.Context, userID, roomID string) (string, error)
}

// ChannelAuthorizer decides who may subscribe to a channel.
//
// The manager consults it on every Subscribe, whether the request came from a
// client (an SSE ?channels= parameter, the subscribe endpoint, a resumed
// session) or from server code. userID is empty for an anonymous connection,
// which reaches the authorizer only when the manager is configured to admit
// anonymous subscribers at all.
type ChannelAuthorizer interface {
// CanSubscribe reports whether userID may subscribe to channelID.
CanSubscribe(ctx context.Context, userID, channelID string) (bool, error)
}

// MessageAuthorizer checks message-level permissions.
type MessageAuthorizer interface {
// CanSend checks if user can send message to room/channel
Expand Down
42 changes: 42 additions & 0 deletions extensions/streaming/backends/local/channel_private_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package local

import (
"context"
"testing"

streaming "github.com/xraph/forge/extensions/streaming/internal"
)

// opaqueChannel is a Channel that is not a *LocalChannel, so Create has to
// copy it.
type opaqueChannel struct {
*LocalChannel
}

// The private flag decides channel authorization, so it has to survive both a
// direct Create and the copy Create makes of a foreign Channel.
func TestChannelStore_KeepsPrivateFlag(t *testing.T) {
ctx := context.Background()
s := NewChannelStore()

direct := NewLocalChannel(streaming.ChannelOptions{ID: "a", Name: "a", Private: true})
copied := opaqueChannel{NewLocalChannel(streaming.ChannelOptions{ID: "b", Name: "b", Private: true})}
public := NewLocalChannel(streaming.ChannelOptions{ID: "c", Name: "c"})

for _, ch := range []streaming.Channel{direct, copied, public} {
if err := s.Create(ctx, ch); err != nil {
t.Fatal(err)
}
}

for id, want := range map[string]bool{"a": true, "b": true, "c": false} {
ch, err := s.Get(ctx, id)
if err != nil {
t.Fatal(err)
}

if got := ch.(*LocalChannel).IsPrivate(); got != want {
t.Errorf("channel %s: IsPrivate = %v, want %v", id, got, want)
}
}
}
19 changes: 19 additions & 0 deletions extensions/streaming/backends/local/channel_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func (s *ChannelStore) Create(ctx context.Context, channel streaming.Channel) er
created: channel.GetCreated(),
messageCount: channel.GetMessageCount(),
}

// Keep the private flag across the copy. Dropping it would quietly
// turn a private channel into a public one.
if p, ok := channel.(interface{ IsPrivate() bool }); ok {
localChannel.private = p.IsPrivate()
}
}

s.channels[localChannel.id] = localChannel
Expand Down Expand Up @@ -260,6 +266,7 @@ type LocalChannel struct {
name string
created time.Time
messageCount int64
private bool
}

func NewLocalChannel(opts streaming.ChannelOptions) *LocalChannel {
Expand All @@ -268,9 +275,21 @@ func NewLocalChannel(opts streaming.ChannelOptions) *LocalChannel {
name: opts.Name,
created: time.Now(),
messageCount: 0,
private: opts.Private,
}
}

// IsPrivate reports whether the channel was created with
// ChannelOptions.Private. The manager's default channel authorizer refuses
// every subscriber to a private channel unless the application supplies an
// authorizer that knows who belongs.
func (c *LocalChannel) IsPrivate() bool {
c.mu.RLock()
defer c.mu.RUnlock()

return c.private
}

func (c *LocalChannel) GetID() string {
c.mu.RLock()
defer c.mu.RUnlock()
Expand Down
Loading
Loading