Skip to content

Commit

Permalink
Merge branch 'staging' into noa/bearer-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
bfops authored Feb 11, 2025
2 parents ed7a3b0 + 2f55054 commit cbcaa35
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 106 deletions.
9 changes: 0 additions & 9 deletions examples~/quickstart/client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ void Main()
conn.Reducers.OnSetName += Reducer_OnSetNameEvent;
conn.Reducers.OnSendMessage += Reducer_OnSendMessageEvent;

#pragma warning disable CS0612 // Using obsolete API
conn.onUnhandledReducerError += onUnhandledReducerError;
#pragma warning restore CS0612 // Using obsolete API

// declare a threadsafe cancel token to cancel the process loop
var cancellationTokenSource = new CancellationTokenSource();

Expand Down Expand Up @@ -176,11 +172,6 @@ void OnSubscriptionApplied(SubscriptionEventContext ctx)
PrintMessagesInOrder(ctx.Db);
}

void onUnhandledReducerError(ReducerEvent<Reducer> reducerEvent)
{
Console.WriteLine($"Unhandled reducer error in {reducerEvent.Reducer}: {reducerEvent.Status}");
}

void ProcessThread(DbConnection conn, CancellationToken ct)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ internal SubscriptionEventContext(DbConnection conn)
public abstract partial class Reducer
{
private Reducer() { }

public sealed class StdbNone : Reducer { }
}

public sealed class DbConnection : DbConnectionBase<DbConnection, RemoteTables, Reducer>
Expand All @@ -125,7 +123,6 @@ protected override Reducer ToReducer(TransactionUpdate update)
"identity_disconnected" => BSATNHelpers.Decode<Reducer.IdentityDisconnected>(encodedArgs),
"send_message" => BSATNHelpers.Decode<Reducer.SendMessage>(encodedArgs),
"set_name" => BSATNHelpers.Decode<Reducer.SetName>(encodedArgs),
"<none>" or "" => new Reducer.StdbNone(),
var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
Expand All @@ -151,7 +148,6 @@ protected override bool Dispatch(IReducerEventContext context, Reducer reducer)
Reducer.IdentityDisconnected args => Reducers.InvokeIdentityDisconnected(eventContext, args),
Reducer.SendMessage args => Reducers.InvokeSendMessage(eventContext, args),
Reducer.SetName args => Reducers.InvokeSetName(eventContext, args),
Reducer.StdbNone => true,
_ => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}")
};
}
Expand Down
Binary file not shown.
Binary file not shown.
50 changes: 17 additions & 33 deletions src/SpacetimeDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ struct DbOp
/// </summary>
private UintAllocator queryIdAllocator;

/// <summary>
/// Invoked when a reducer is returned with an error and has no client-side handler.
/// </summary>
[Obsolete]
public event Action<ReducerEvent<Reducer>>? onUnhandledReducerError;

public readonly ConnectionId ConnectionId = ConnectionId.Random();
public Identity? Identity { get; private set; }

Expand Down Expand Up @@ -617,9 +611,11 @@ PreProcessedMessage PreProcessMessage(UnprocessedMessage unprocessed)
transactionUpdate.EnergyQuantaUsed.Quanta,
ToReducer(transactionUpdate));
}
catch (Exception e)
catch (Exception)
{
Log.Exception(e);
// Failing to parse the ReducerEvent is fine, it just means we should
// call downstream stuff with an UnknownTransaction.
// See OnProcessMessageComplete.
}

if (transactionUpdate.Status is UpdateStatus.Committed(var committed))
Expand Down Expand Up @@ -697,6 +693,9 @@ void IDbConnection.Connect(string? token, string uri, string addressOrName, Comp
{
uri = $"ws://{uri}";
}
// Things fail surprisingly if we have a trailing slash, because we later manually append strings
// like `/foo` and then end up with `//` in the URI.
uri = uri.TrimEnd('/');

Log.Info($"SpacetimeDBClient: Connecting to {uri} {addressOrName}");
if (!IsTesting)
Expand Down Expand Up @@ -809,6 +808,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
var eventContext = MakeSubscriptionEventContext();
var legacyEventContext = ToEventContext(new Event<Reducer>.SubscribeApplied());
OnMessageProcessCompleteUpdate(legacyEventContext, dbOps);

if (legacySubscriptions.TryGetValue(initialSubscription.RequestId, out var subscription))
{
try
Expand Down Expand Up @@ -929,36 +929,20 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
}
}

if (processed.reducerEvent is not { } reducerEvent)
{
// If we are here, an error about unknown reducer should have already been logged, so nothing to do.
break;
}

var eventContext = ToReducerEventContext(reducerEvent);
var legacyEventContext = ToEventContext(new Event<Reducer>.Reducer(reducerEvent));
OnMessageProcessCompleteUpdate(legacyEventContext, dbOps);

var reducerFound = false;
try
if (processed.reducerEvent is { } reducerEvent)
{
reducerFound = Dispatch(eventContext, reducerEvent.Reducer);
var legacyEventContext = ToEventContext(new Event<Reducer>.Reducer(reducerEvent));
OnMessageProcessCompleteUpdate(legacyEventContext, dbOps);
var eventContext = ToReducerEventContext(reducerEvent);
Dispatch(eventContext, reducerEvent.Reducer);
// don't invoke OnUnhandledReducerError, that's [Obsolete].
}
catch (Exception e)
{
Log.Exception(e);
}

if (!reducerFound && transactionUpdate.Status is UpdateStatus.Failed(var failed))
else
{
try
{
onUnhandledReducerError?.Invoke(reducerEvent);
}
catch (Exception e)
{
Log.Exception(e);
}
var legacyEventContext = ToEventContext(new Event<Reducer>.UnknownTransaction());
OnMessageProcessCompleteUpdate(legacyEventContext, dbOps);
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public WebSocket(ConnectOptions options)

public async Task Connect(string? auth, string host, string nameOrAddress, ConnectionId connectionId, Compression compression, bool light)
{
var uri = $"{host}/database/subscribe/{nameOrAddress}?connection_id={connectionId}&compression={compression}";
var uri = $"{host}/v1/database/{nameOrAddress}/subscribe?connection_id={connectionId}&compression={compression}";
if (light)
{
uri += "&light=true";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@
online: true
}
},
LogException: Unknown reducer unknown-reducer (Parameter 'Reducer'),
OnInsertUser: {
eventContext: {
Event: {
$type: Event<Reducer>.UnknownTransaction
},
Db: {Scrubbed},
Reducers: {Scrubbed},
SetReducerFlags: {}
},
user: {
identity: Identity_2,
online: true
}
},
OnInsertUser: {
eventContext: {
Event: {
Expand All @@ -29,7 +42,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -42,7 +55,7 @@
SetReducerFlags: {}
},
user: {
identity: Identity_2,
identity: Identity_3,
online: true
}
},
Expand Down Expand Up @@ -98,7 +111,7 @@
},
User: {
Identity: {},
Count: 2
Count: 3
}
},
Reducers: {},
Expand All @@ -113,7 +126,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -127,7 +140,7 @@
SetReducerFlags: {}
},
message: {
sender: Identity_2,
sender: Identity_3,
sent: 1718487775346381,
text: Hello, A!
}
Expand All @@ -138,7 +151,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -152,7 +165,7 @@
},
User: {
Identity: {},
Count: 2
Count: 3
}
},
Reducers: {},
Expand All @@ -167,7 +180,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -181,11 +194,11 @@
SetReducerFlags: {}
},
oldUser: {
identity: Identity_2,
identity: Identity_3,
online: true
},
newUser: {
identity: Identity_2,
identity: Identity_3,
name: B,
online: true
}
Expand All @@ -196,7 +209,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -210,7 +223,7 @@
},
User: {
Identity: {},
Count: 2
Count: 3
}
},
Reducers: {},
Expand Down Expand Up @@ -264,7 +277,7 @@
},
User: {
Identity: {},
Count: 2
Count: 3
}
},
Reducers: {},
Expand All @@ -279,7 +292,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -293,7 +306,7 @@
SetReducerFlags: {}
},
message: {
sender: Identity_2,
sender: Identity_3,
sent: 1718487787645364,
text: Goodbye!
}
Expand All @@ -304,7 +317,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -318,7 +331,7 @@
},
User: {
Identity: {},
Count: 2
Count: 3
}
},
Reducers: {},
Expand All @@ -333,7 +346,7 @@
Status: {
$type: Status.Committed
},
CallerIdentity: Identity_2,
CallerIdentity: Identity_3,
CallerConnectionId: Guid_1,
EnergyConsumed: {},
Reducer: {
Expand All @@ -346,12 +359,12 @@
SetReducerFlags: {}
},
oldUser: {
identity: Identity_2,
identity: Identity_3,
name: B,
online: true
},
newUser: {
identity: Identity_2,
identity: Identity_3,
name: B,
online: false
}
Expand Down Expand Up @@ -404,7 +417,7 @@
},
User: {
Identity: {},
Count: 2
Count: 3
}
},
Reducers: {},
Expand All @@ -420,13 +433,17 @@
},
{
identity: Identity_2,
online: true
},
{
identity: Identity_3,
name: B,
online: false
}
],
Message: [
{
sender: Identity_2,
sender: Identity_3,
sent: 1718487775346381,
text: Hello, A!
},
Expand All @@ -436,7 +453,7 @@
text: Hello, B!
},
{
sender: Identity_2,
sender: Identity_3,
sent: 1718487787645364,
text: Goodbye!
},
Expand Down
Loading

0 comments on commit cbcaa35

Please sign in to comment.