예제 #1
0
        private async Task WriteMessage(IServiceConnectionContainer connection, AppMessage appMessage)
        {
            var message = appMessage.Message;

            try
            {
                switch (message)
                {
                // For group related messages, make sure messages are written to the same partition
                case JoinGroupWithAckMessage joinGroupMessage:
                    try
                    {
                        await connection.WriteAckableMessageAsync(joinGroupMessage);
                    }
                    finally
                    {
                        _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                    }
                    break;

                case LeaveGroupWithAckMessage leaveGroupMessage:
                    try
                    {
                        await connection.WriteAckableMessageAsync(leaveGroupMessage);
                    }
                    finally
                    {
                        _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                    }
                    break;

                case ConnectionDataMessage connectionDataMessage:
                    var connectionId = connectionDataMessage.ConnectionId;
                    if (_clientConnectionManager.TryGetClientConnection(connectionId, out var conn))
                    {
                        // If the client connection is connected to local server connection,
                        // send back directly from the established server connection
                        await conn.WriteMessageAsync(connectionDataMessage);
                    }
                    else
                    {
                        await connection.WriteAsync(message);
                    }
                    break;

                default:
                    await connection.WriteAsync(message);

                    break;
                }

                if (message is IMessageWithTracingId msg && msg.TracingId != null)
                {
                    MessageLog.SucceededToSendMessage(_logger, msg);
                }
            }
            catch (Exception ex)
            {
                if (message is IMessageWithTracingId msg && msg.TracingId != null)
                {
                    MessageLog.FailedToSendMessage(_logger, msg, ex);
                }
                throw;
            }
        }