-
Notifications
You must be signed in to change notification settings - Fork 371
Add dead letter topic support #929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // ------------------------------------------------------------------------ | ||
| // ------------------------------------------------------------------------ | ||
| // Copyright 2021 The Dapr Authors | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
|
|
@@ -16,13 +16,15 @@ namespace RoutingSample | |
| using System; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Dapr; | ||
| using Dapr.Client; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Configuration; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| /// <summary> | ||
| /// Startup class. | ||
|
|
@@ -74,7 +76,8 @@ public void ConfigureServices(IServiceCollection services) | |
| /// <param name="app">Application builder.</param> | ||
| /// <param name="env">Webhost environment.</param> | ||
| /// <param name="serializerOptions">Options for JSON serialization.</param> | ||
| public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSerializerOptions serializerOptions) | ||
| public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSerializerOptions serializerOptions, | ||
| ILogger<Startup> logger) | ||
| { | ||
| if (env.IsDevelopment()) | ||
| { | ||
|
|
@@ -89,40 +92,52 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, JsonSeri | |
| { | ||
| endpoints.MapSubscribeHandler(); | ||
|
|
||
| var depositTopicOptions = new TopicOptions(); | ||
| depositTopicOptions.PubsubName = PubsubName; | ||
| depositTopicOptions.Name = "deposit"; | ||
| depositTopicOptions.DeadLetterTopic = "amountDeadLetterTopic"; | ||
|
|
||
| var withdrawTopicOptions = new TopicOptions(); | ||
| withdrawTopicOptions.PubsubName = PubsubName; | ||
| withdrawTopicOptions.Name = "withdraw"; | ||
| withdrawTopicOptions.DeadLetterTopic = "amountDeadLetterTopic"; | ||
|
Comment on lines
95
to
103
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these duplicates to the attributes?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just realized this was in the |
||
|
|
||
| endpoints.MapGet("{id}", Balance); | ||
| endpoints.MapPost("deposit", Deposit).WithTopic(PubsubName, "deposit"); | ||
| endpoints.MapPost("withdraw", Withdraw).WithTopic(PubsubName, "withdraw"); | ||
| endpoints.MapPost("deposit", Deposit).WithTopic(depositTopicOptions); | ||
| endpoints.MapPost("deadLetterTopicRoute", ViewErrorMessage).WithTopic(PubsubName, "amountDeadLetterTopic"); | ||
| endpoints.MapPost("withdraw", Withdraw).WithTopic(withdrawTopicOptions); | ||
| }); | ||
|
|
||
| async Task Balance(HttpContext context) | ||
| { | ||
| Console.WriteLine("Enter Balance"); | ||
| logger.LogDebug("Enter Balance"); | ||
| var client = context.RequestServices.GetRequiredService<DaprClient>(); | ||
|
|
||
| var id = (string)context.Request.RouteValues["id"]; | ||
| Console.WriteLine("id is {0}", id); | ||
| logger.LogDebug("id is {0}", id); | ||
| var account = await client.GetStateAsync<Account>(StoreName, id); | ||
| if (account == null) | ||
| { | ||
| Console.WriteLine("Account not found"); | ||
| logger.LogDebug("Account not found"); | ||
| context.Response.StatusCode = 404; | ||
| return; | ||
| } | ||
|
|
||
| Console.WriteLine("Account balance is {0}", account.Balance); | ||
| logger.LogDebug("Account balance is {0}", account.Balance); | ||
|
|
||
| context.Response.ContentType = "application/json"; | ||
| await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions); | ||
| } | ||
|
|
||
| async Task Deposit(HttpContext context) | ||
| { | ||
| Console.WriteLine("Enter Deposit"); | ||
|
|
||
| var client = context.RequestServices.GetRequiredService<DaprClient>(); | ||
| logger.LogDebug("Enter Deposit"); | ||
|
|
||
| var client = context.RequestServices.GetRequiredService<DaprClient>(); | ||
| var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions); | ||
| Console.WriteLine("Id is {0}, Amount is {1}", transaction.Id, transaction.Amount); | ||
|
|
||
| logger.LogDebug("Id is {0}, Amount is {1}", transaction.Id, transaction.Amount); | ||
|
|
||
| var account = await client.GetStateAsync<Account>(StoreName, transaction.Id); | ||
| if (account == null) | ||
| { | ||
|
|
@@ -131,43 +146,56 @@ async Task Deposit(HttpContext context) | |
|
|
||
| if (transaction.Amount < 0m) | ||
| { | ||
| Console.WriteLine("Invalid amount"); | ||
| logger.LogDebug("Invalid amount"); | ||
| context.Response.StatusCode = 400; | ||
| return; | ||
| } | ||
|
|
||
| account.Balance += transaction.Amount; | ||
| await client.SaveStateAsync(StoreName, transaction.Id, account); | ||
| Console.WriteLine("Balance is {0}", account.Balance); | ||
| logger.LogDebug("Balance is {0}", account.Balance); | ||
|
|
||
| context.Response.ContentType = "application/json"; | ||
| await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions); | ||
| } | ||
|
|
||
| async Task ViewErrorMessage(HttpContext context) | ||
| { | ||
| var client = context.RequestServices.GetRequiredService<DaprClient>(); | ||
| var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions); | ||
|
|
||
| logger.LogDebug("The amount cannot be negative: {0}", transaction.Amount); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| async Task Withdraw(HttpContext context) | ||
| { | ||
| Console.WriteLine("Enter Withdraw"); | ||
| logger.LogDebug("Enter Withdraw"); | ||
|
|
||
| var client = context.RequestServices.GetRequiredService<DaprClient>(); | ||
| var transaction = await JsonSerializer.DeserializeAsync<Transaction>(context.Request.Body, serializerOptions); | ||
| Console.WriteLine("Id is {0}", transaction.Id); | ||
|
|
||
| logger.LogDebug("Id is {0}, Amount is {1}", transaction.Id, transaction.Amount); | ||
|
|
||
| var account = await client.GetStateAsync<Account>(StoreName, transaction.Id); | ||
| if (account == null) | ||
| { | ||
| Console.WriteLine("Account not found"); | ||
| logger.LogDebug("Account not found"); | ||
| context.Response.StatusCode = 404; | ||
| return; | ||
| } | ||
|
|
||
| if (transaction.Amount < 0m) | ||
| { | ||
| Console.WriteLine("Invalid amount"); | ||
| logger.LogDebug("Invalid amount"); | ||
| context.Response.StatusCode = 400; | ||
| return; | ||
| } | ||
|
|
||
| account.Balance -= transaction.Amount; | ||
| await client.SaveStateAsync(StoreName, transaction.Id, account); | ||
| Console.WriteLine("Balance is {0}", account.Balance); | ||
| logger.LogDebug("Balance is {0}", account.Balance); | ||
|
|
||
| context.Response.ContentType = "application/json"; | ||
| await JsonSerializer.SerializeAsync(context.Response.Body, account, serializerOptions); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were there no changes needed for the program that runs the example? Or updates to the README that explains it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll probably add a small section to test the dead letter topic by providing a negative amount to be deposited/withdrawn