Jelajahi Sumber

Update the connection ID on transient failures (#969)

Motivation:

The logger for the connection mananger includes some metadata for the
connection ID, and the connection mananger is willing to append that
metadata to any logger provided to it.

The ID is a UUID suffixed with a monotonically increasing
number. The suffix is incremented after the connection has become idle
so that new channels have a unique but related ID. However, if a
connection is dropped the connection manager will attempt to
re-establish it yet the suffix will not change. This is quite
misleading in logs since the underlying channel will have changed.

Modifications:

- Increment the connection number when transitioning to transient
  failure

Result:

- The connection ID is incremented when the underlying channel will
  change
George Barnett 5 tahun lalu
induk
melakukan
15f28ce4ad
1 mengubah file dengan 14 tambahan dan 7 penghapusan
  1. 14 7
      Sources/GRPC/ConnectionManager.swift

+ 14 - 7
Sources/GRPC/ConnectionManager.swift

@@ -164,12 +164,7 @@ internal class ConnectionManager {
       switch self.state {
       case .idle:
         self.monitor.updateState(to: .idle, logger: self.logger)
-
-        // Create a new id; it'll be used for the *next* channel we create.
-        self.channelNumberLock.withLockVoid {
-          self.channelNumber &+= 1
-        }
-        self.logger[metadataKey: MetadataKey.connectionID] = "\(self.connectionIDAndNumber)"
+        self.updateConnectionID()
 
       case .connecting:
         self.monitor.updateState(to: .connecting, logger: self.logger)
@@ -183,6 +178,7 @@ internal class ConnectionManager {
 
       case .transientFailure:
         self.monitor.updateState(to: .transientFailure, logger: self.logger)
+        self.updateConnectionID()
 
       case .shutdown:
         self.monitor.updateState(to: .shutdown, logger: self.logger)
@@ -198,9 +194,20 @@ internal class ConnectionManager {
   private var channelNumber: UInt64
   private var channelNumberLock = Lock()
 
+  private var _connectionIDAndNumber: String {
+    return "\(self.connectionID)/\(self.channelNumber)"
+  }
+
   private var connectionIDAndNumber: String {
     return self.channelNumberLock.withLock {
-      return "\(self.connectionID)/\(self.channelNumber)"
+      return self._connectionIDAndNumber
+    }
+  }
+
+  private func updateConnectionID() {
+    self.channelNumberLock.withLockVoid {
+      self.channelNumber &+= 1
+      self.logger[metadataKey: MetadataKey.connectionID] = "\(self._connectionIDAndNumber)"
     }
   }