Skip to content

Commit 22d1469

Browse files
committed
Task 1
1 parent 5017bd4 commit 22d1469

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Threading;
18+
using System.Threading.Tasks;
19+
20+
namespace Amazon.Lambda.RuntimeSupport
21+
{
22+
/// <summary>
23+
/// Interface for writing streaming responses in AWS Lambda functions.
24+
/// Obtained by calling ResponseStreamFactory.CreateStream() within a handler.
25+
/// </summary>
26+
public interface IResponseStream : IDisposable
27+
{
28+
/// <summary>
29+
/// Asynchronously writes a byte array to the response stream.
30+
/// </summary>
31+
/// <param name="buffer">The byte array to write.</param>
32+
/// <param name="cancellationToken">Optional cancellation token.</param>
33+
/// <returns>A task representing the asynchronous operation.</returns>
34+
/// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has been reported.</exception>
35+
/// <exception cref="InvalidOperationException">Thrown if writing would exceed the 20 MiB limit.</exception>
36+
Task WriteAsync(byte[] buffer, CancellationToken cancellationToken = default);
37+
38+
/// <summary>
39+
/// Asynchronously writes a portion of a byte array to the response stream.
40+
/// </summary>
41+
/// <param name="buffer">The byte array containing data to write.</param>
42+
/// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes.</param>
43+
/// <param name="count">The number of bytes to write.</param>
44+
/// <param name="cancellationToken">Optional cancellation token.</param>
45+
/// <returns>A task representing the asynchronous operation.</returns>
46+
/// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has been reported.</exception>
47+
/// <exception cref="InvalidOperationException">Thrown if writing would exceed the 20 MiB limit.</exception>
48+
Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default);
49+
50+
/// <summary>
51+
/// Asynchronously writes a memory buffer to the response stream.
52+
/// </summary>
53+
/// <param name="buffer">The memory buffer to write.</param>
54+
/// <param name="cancellationToken">Optional cancellation token.</param>
55+
/// <returns>A task representing the asynchronous operation.</returns>
56+
/// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has been reported.</exception>
57+
/// <exception cref="InvalidOperationException">Thrown if writing would exceed the 20 MiB limit.</exception>
58+
Task WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default);
59+
60+
/// <summary>
61+
/// Reports an error that occurred during streaming.
62+
/// This will send error information via HTTP trailing headers.
63+
/// </summary>
64+
/// <param name="exception">The exception to report.</param>
65+
/// <param name="cancellationToken">Optional cancellation token.</param>
66+
/// <returns>A task representing the asynchronous operation.</returns>
67+
/// <exception cref="InvalidOperationException">Thrown if the stream is already completed or an error has already been reported.</exception>
68+
Task ReportErrorAsync(Exception exception, CancellationToken cancellationToken = default);
69+
70+
/// <summary>
71+
/// Gets the total number of bytes written to the stream so far.
72+
/// </summary>
73+
long BytesWritten { get; }
74+
75+
/// <summary>
76+
/// Gets whether the stream has been completed.
77+
/// </summary>
78+
bool IsCompleted { get; }
79+
80+
/// <summary>
81+
/// Gets whether an error has been reported.
82+
/// </summary>
83+
bool HasError { get; }
84+
}
85+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
namespace Amazon.Lambda.RuntimeSupport
17+
{
18+
/// <summary>
19+
/// Internal context class used by ResponseStreamFactory to track per-invocation streaming state.
20+
/// </summary>
21+
internal class ResponseStreamContext
22+
{
23+
/// <summary>
24+
/// The AWS request ID for the current invocation.
25+
/// </summary>
26+
public string AwsRequestId { get; set; }
27+
28+
/// <summary>
29+
/// Maximum allowed response size in bytes (20 MiB).
30+
/// </summary>
31+
public long MaxResponseSize { get; set; }
32+
33+
/// <summary>
34+
/// Whether CreateStream() has been called for this invocation.
35+
/// </summary>
36+
public bool StreamCreated { get; set; }
37+
38+
/// <summary>
39+
/// The IResponseStream instance if created. Typed as IResponseStream for now;
40+
/// will be used with the concrete ResponseStream internally.
41+
/// </summary>
42+
public IResponseStream Stream { get; set; }
43+
}
44+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
namespace Amazon.Lambda.RuntimeSupport
17+
{
18+
/// <summary>
19+
/// Constants used for Lambda response streaming.
20+
/// </summary>
21+
internal static class StreamingConstants
22+
{
23+
/// <summary>
24+
/// Maximum response size for Lambda streaming responses: 20 MiB.
25+
/// </summary>
26+
public const long MaxResponseSize = 20 * 1024 * 1024;
27+
28+
/// <summary>
29+
/// Header name for Lambda response mode.
30+
/// </summary>
31+
public const string ResponseModeHeader = "Lambda-Runtime-Function-Response-Mode";
32+
33+
/// <summary>
34+
/// Value for streaming response mode.
35+
/// </summary>
36+
public const string StreamingResponseMode = "streaming";
37+
38+
/// <summary>
39+
/// Trailer header name for error type.
40+
/// </summary>
41+
public const string ErrorTypeTrailer = "Lambda-Runtime-Function-Error-Type";
42+
43+
/// <summary>
44+
/// Trailer header name for error body.
45+
/// </summary>
46+
public const string ErrorBodyTrailer = "Lambda-Runtime-Function-Error-Body";
47+
}
48+
}

0 commit comments

Comments
 (0)