| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | #nullable enable |
| | 4 | |
|
| | 5 | | using System.Diagnostics; |
| | 6 | |
|
| | 7 | | namespace Ice.Internal; |
| | 8 | |
|
| | 9 | | internal sealed class RequestHandlerCache |
| | 10 | | { |
| | 11 | | internal RequestHandler? requestHandler |
| | 12 | | { |
| | 13 | | get |
| | 14 | | { |
| 1 | 15 | | if (_cacheConnection) |
| | 16 | | { |
| 1 | 17 | | lock (_mutex) |
| | 18 | | { |
| 1 | 19 | | if (_cachedRequestHandler != null) |
| | 20 | | { |
| 1 | 21 | | return _cachedRequestHandler; |
| | 22 | | } |
| 1 | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| 1 | 26 | | RequestHandler handler = _reference.getRequestHandler(); |
| 1 | 27 | | if (_cacheConnection) |
| | 28 | | { |
| 1 | 29 | | lock (_mutex) |
| | 30 | | { |
| 1 | 31 | | _cachedRequestHandler ??= handler; |
| | 32 | | // else ignore handler |
| 1 | 33 | | return _cachedRequestHandler; |
| | 34 | | } |
| | 35 | | } |
| | 36 | | else |
| | 37 | | { |
| 1 | 38 | | return handler; |
| | 39 | | } |
| 1 | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | internal Connection? cachedConnection |
| | 44 | | { |
| | 45 | | get |
| | 46 | | { |
| 1 | 47 | | if (_cacheConnection) |
| | 48 | | { |
| | 49 | | RequestHandler? handler; |
| 1 | 50 | | lock (_mutex) |
| | 51 | | { |
| 1 | 52 | | handler = _cachedRequestHandler; |
| 1 | 53 | | } |
| 1 | 54 | | if (handler is not null) |
| | 55 | | { |
| 1 | 56 | | return handler.getConnection(); |
| | 57 | | } |
| | 58 | | } |
| 1 | 59 | | return null; |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| | 63 | | private readonly Reference _reference; |
| | 64 | | private readonly bool _cacheConnection; |
| 1 | 65 | | private readonly object _mutex = new(); |
| | 66 | | private RequestHandler? _cachedRequestHandler; |
| | 67 | |
|
| 1 | 68 | | internal RequestHandlerCache(Reference reference) |
| | 69 | | { |
| 1 | 70 | | _reference = reference; |
| 1 | 71 | | _cacheConnection = reference.getCacheConnection(); |
| 1 | 72 | | } |
| | 73 | |
|
| | 74 | | internal void clearCachedRequestHandler(RequestHandler handler) |
| | 75 | | { |
| 1 | 76 | | if (_cacheConnection) |
| | 77 | | { |
| 1 | 78 | | lock (_mutex) |
| | 79 | | { |
| 1 | 80 | | if (handler == _cachedRequestHandler) |
| | 81 | | { |
| 1 | 82 | | _cachedRequestHandler = null; |
| | 83 | | } |
| 1 | 84 | | } |
| | 85 | | } |
| 1 | 86 | | } |
| | 87 | | } |