-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[Server] Make EndpointIncomingRequest a ReadonlyStruct & pool ValueTaks #3363
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
marcschier
merged 7 commits into
OPCFoundation:master
from
romanett:feat/ReadOnlyStructIncomingRequest
Dec 2, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1205541
Make EndpointIncomingRequest a readonly struct
romanett 5c39d0e
remove asyncresultbase as now broken
romanett 5f00e73
Switch to ValueTask & pool ValueTasks using ValueTaskSource
romanett 99d7b60
cleanup
romanett d100546
Use specific ValueTaskSource with integrated pooling to ensure concur…
romanett 5486714
update
romanett 247e1c0
improve load test stability
romanett 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
73 changes: 73 additions & 0 deletions
73
Stack/Opc.Ua.Core/Types/Utils/ValueTask/ManualResetValueTaskSource.cs
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 |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* Copyright (c) 1996-2022 The OPC Foundation. All rights reserved. | ||
| The source code in this file is covered under a dual-license scenario: | ||
| - RCL: for OPC Foundation Corporate Members in good-standing | ||
| - GPL V2: everybody else | ||
| RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/ | ||
| GNU General Public License as published by the Free Software Foundation; | ||
| version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2 | ||
| This source code is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using System.Threading.Tasks.Sources; | ||
|
|
||
| namespace Opc.Ua | ||
| { | ||
| /// <summary> | ||
| /// A reusable value task source. | ||
| /// </summary> | ||
| /// <typeparam name="T"></typeparam> | ||
| internal class ManualResetValueTaskSource<T> : IValueTaskSource<T>, IValueTaskSource | ||
| { | ||
| private ManualResetValueTaskSourceCore<T> m_core; | ||
|
|
||
| public bool RunContinuationsAsynchronously | ||
| { | ||
| get => m_core.RunContinuationsAsynchronously; | ||
| set => m_core.RunContinuationsAsynchronously = value; | ||
| } | ||
|
|
||
| public short Version => m_core.Version; | ||
|
|
||
| public void Reset() | ||
| { | ||
| m_core.Reset(); | ||
| } | ||
|
|
||
| public void SetResult(T result) | ||
| { | ||
| m_core.SetResult(result); | ||
| } | ||
|
|
||
| public void SetException(Exception error) | ||
| { | ||
| m_core.SetException(error); | ||
| } | ||
|
|
||
| public T GetResult(short token) | ||
| { | ||
| return m_core.GetResult(token); | ||
| } | ||
|
|
||
| void IValueTaskSource.GetResult(short token) | ||
| { | ||
| m_core.GetResult(token); | ||
| } | ||
|
|
||
| public ValueTaskSourceStatus GetStatus(short token) | ||
| { | ||
| return m_core.GetStatus(token); | ||
| } | ||
|
|
||
| public void OnCompleted(Action<object> continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) | ||
| { | ||
| m_core.OnCompleted(continuation, state, token, flags); | ||
| } | ||
|
|
||
| public ValueTask<T> Task => new(this, m_core.Version); | ||
| public ValueTask SourceTask => new(this, m_core.Version); | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* Copyright (c) 1996-2022 The OPC Foundation. All rights reserved. | ||
| The source code in this file is covered under a dual-license scenario: | ||
| - RCL: for OPC Foundation Corporate Members in good-standing | ||
| - GPL V2: everybody else | ||
| RCL license terms accompanied with this source code. See http://opcfoundation.org/License/RCL/1.00/ | ||
| GNU General Public License as published by the Free Software Foundation; | ||
| version 2 of the License are accompanied with this source code. See http://opcfoundation.org/License/GPLv2 | ||
| This source code is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| */ | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
|
|
||
| namespace Opc.Ua | ||
| { | ||
| /// <summary> | ||
| /// A simple object pool implementation. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of object to pool.</typeparam> | ||
| internal class ObjectPool<T> where T : class | ||
| { | ||
| private readonly ConcurrentBag<T> m_objects; | ||
| private readonly Func<T> m_objectGenerator; | ||
| private readonly int m_maxSize; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ObjectPool{T}"/> class. | ||
| /// </summary> | ||
| /// <param name="objectGenerator">The function to generate new objects.</param> | ||
| /// <param name="maxSize">The maximum size of the pool.</param> | ||
| public ObjectPool(Func<T> objectGenerator, int maxSize) | ||
| { | ||
| m_objectGenerator = objectGenerator ?? throw new ArgumentNullException(nameof(objectGenerator)); | ||
| m_maxSize = maxSize > 0 ? maxSize : throw new ArgumentOutOfRangeException(nameof(maxSize)); | ||
| m_objects = new ConcurrentBag<T>(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets an object from the pool. | ||
| /// </summary> | ||
| /// <returns>An object from the pool or a new one if the pool is empty.</returns> | ||
| public T Get() | ||
| { | ||
| if (m_objects.TryTake(out T item)) | ||
| { | ||
| return item; | ||
| } | ||
|
|
||
| return m_objectGenerator(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns an object to the pool. | ||
| /// </summary> | ||
| /// <param name="item">The object to return.</param> | ||
| public void Return(T item) | ||
| { | ||
| if (m_objects.Count < m_maxSize) | ||
| { | ||
| m_objects.Add(item); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Race condition: Multiple threads could simultaneously check
m_objects.Count < m_maxSizeand all pass the check before any adds the item, potentially allowing the pool to grow beyondm_maxSize. Consider using a counter withInterlocked.Incrementbefore adding, and only add if the incremented count is <= maxSize, otherwise decrement back.