|
| 1 | +using System; |
| 2 | +using System.Threading; |
| 3 | + |
| 4 | +namespace GVFS.Common.Tracing |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Manages deferred telemetry pipe attachment for processes that cannot |
| 8 | + /// read the pipe config at startup (e.g. GVFS.Service running as SYSTEM, |
| 9 | + /// or any process started before the telemetry collector is installed). |
| 10 | + /// |
| 11 | + /// Adds a <see cref="BufferingTelemetryListener"/> to the tracer at |
| 12 | + /// construction time, then periodically retries creating a real |
| 13 | + /// <see cref="TelemetryDaemonEventListener"/>. On success, buffered |
| 14 | + /// messages are replayed and the retry timer stops. |
| 15 | + /// |
| 16 | + /// Callers can also trigger an explicit attach attempt via |
| 17 | + /// <see cref="TryAttach(string)"/> — e.g. on session logon when the |
| 18 | + /// user's HOME is available. |
| 19 | + /// |
| 20 | + /// Designed for reuse by both GVFS.Service and GVFS.Mount. |
| 21 | + /// </summary> |
| 22 | + public class DeferredTelemetryAttacher : IDisposable |
| 23 | + { |
| 24 | + private readonly JsonTracer tracer; |
| 25 | + private readonly BufferingTelemetryListener buffer; |
| 26 | + private readonly string providerName; |
| 27 | + private readonly string enlistmentId; |
| 28 | + private readonly string mountId; |
| 29 | + private readonly Lock attachLock = new Lock(); |
| 30 | + |
| 31 | + private Timer retryTimer; |
| 32 | + private string retryGitBinRoot; |
| 33 | + private int retryCount; |
| 34 | + private bool attached; |
| 35 | + private bool disposed; |
| 36 | + |
| 37 | + public DeferredTelemetryAttacher( |
| 38 | + JsonTracer tracer, |
| 39 | + string providerName, |
| 40 | + string enlistmentId, |
| 41 | + string mountId) |
| 42 | + { |
| 43 | + this.tracer = tracer; |
| 44 | + this.providerName = providerName; |
| 45 | + this.enlistmentId = enlistmentId; |
| 46 | + this.mountId = mountId; |
| 47 | + this.buffer = new BufferingTelemetryListener(); |
| 48 | + tracer.AddEventListener(this.buffer); |
| 49 | + } |
| 50 | + |
| 51 | + public bool IsAttached |
| 52 | + { |
| 53 | + get |
| 54 | + { |
| 55 | + lock (this.attachLock) |
| 56 | + { |
| 57 | + return this.attached; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Starts a background retry timer that periodically calls |
| 64 | + /// <see cref="TryAttach"/> with the given gitBinRoot. Uses |
| 65 | + /// exponential backoff: 10s, 30s, 1m, then 5m steady state. |
| 66 | + /// </summary> |
| 67 | + public void StartRetryTimer(string gitBinRoot) |
| 68 | + { |
| 69 | + lock (this.attachLock) |
| 70 | + { |
| 71 | + if (this.attached || this.disposed || this.retryTimer != null) |
| 72 | + { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + this.retryGitBinRoot = gitBinRoot; |
| 77 | + this.retryCount = 0; |
| 78 | + this.retryTimer = new Timer( |
| 79 | + this.OnRetryTimer, |
| 80 | + null, |
| 81 | + GetRetryInterval(0), |
| 82 | + Timeout.Infinite); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Attempts to create and attach a TelemetryDaemonEventListener. |
| 88 | + /// Call this when environment conditions change (e.g. user session |
| 89 | + /// becomes available). Replays buffered messages on success. |
| 90 | + /// Safe to call multiple times — no-ops after first successful attach. |
| 91 | + /// </summary> |
| 92 | + /// <param name="gitBinRoot">Path to git binary.</param> |
| 93 | + /// <param name="globalConfigPath"> |
| 94 | + /// If non-null, reads this file with <c>git config --file</c> instead |
| 95 | + /// of <c>--global</c>. Use this when the caller needs to read another |
| 96 | + /// user's .gitconfig without mutating the process-wide HOME variable. |
| 97 | + /// </param> |
| 98 | + /// <returns>true if attached (now or previously).</returns> |
| 99 | + public bool TryAttach(string gitBinRoot, string globalConfigPath = null) |
| 100 | + { |
| 101 | + lock (this.attachLock) |
| 102 | + { |
| 103 | + if (this.attached || this.tracer.HasTelemetryDaemonListener) |
| 104 | + { |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + if (string.IsNullOrEmpty(gitBinRoot)) |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + TelemetryDaemonEventListener daemonListener; |
| 114 | + try |
| 115 | + { |
| 116 | + daemonListener = TelemetryDaemonEventListener.CreateIfEnabled( |
| 117 | + gitBinRoot, |
| 118 | + this.providerName, |
| 119 | + this.enlistmentId, |
| 120 | + this.mountId, |
| 121 | + this.tracer, |
| 122 | + globalConfigPath); |
| 123 | + } |
| 124 | + catch (Exception) |
| 125 | + { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if (daemonListener == null) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + this.tracer.AddEventListener(daemonListener); |
| 135 | + int replayed = this.buffer.ReplayAndStop(daemonListener); |
| 136 | + this.StopRetryTimer(); |
| 137 | + this.attached = true; |
| 138 | + |
| 139 | + this.tracer.RelatedInfo( |
| 140 | + "DeferredTelemetryAttacher: Attached, replayed {0} buffered messages", |
| 141 | + replayed); |
| 142 | + |
| 143 | + return true; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public void Dispose() |
| 148 | + { |
| 149 | + lock (this.attachLock) |
| 150 | + { |
| 151 | + if (this.disposed) |
| 152 | + { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + this.disposed = true; |
| 157 | + this.StopRetryTimer(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + internal static int GetRetryInterval(int retryCount) |
| 162 | + { |
| 163 | + return retryCount switch |
| 164 | + { |
| 165 | + 0 => 10_000, // 10 seconds |
| 166 | + 1 => 30_000, // 30 seconds |
| 167 | + 2 => 60_000, // 1 minute |
| 168 | + _ => 300_000, // 5 minutes |
| 169 | + }; |
| 170 | + } |
| 171 | + |
| 172 | + private void StopRetryTimer() |
| 173 | + { |
| 174 | + // Must be called while holding attachLock |
| 175 | + if (this.retryTimer != null) |
| 176 | + { |
| 177 | + this.retryTimer.Dispose(); |
| 178 | + this.retryTimer = null; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private void OnRetryTimer(object state) |
| 183 | + { |
| 184 | + try |
| 185 | + { |
| 186 | + bool success = this.TryAttach(this.retryGitBinRoot); |
| 187 | + if (!success) |
| 188 | + { |
| 189 | + lock (this.attachLock) |
| 190 | + { |
| 191 | + if (this.retryTimer != null && !this.disposed) |
| 192 | + { |
| 193 | + this.retryCount++; |
| 194 | + this.retryTimer.Change( |
| 195 | + GetRetryInterval(this.retryCount), |
| 196 | + Timeout.Infinite); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + catch (Exception) |
| 202 | + { |
| 203 | + // Swallow — timer will not reschedule, but the explicit |
| 204 | + // TryAttach path (e.g. on SessionLogon) can still succeed. |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments