feat: add client name option for user identification in system events
This commit is contained in:
@@ -31,8 +31,16 @@ public class NatsClientOptions {
|
|||||||
private var clientCertificate: URL? = nil
|
private var clientCertificate: URL? = nil
|
||||||
private var clientKey: URL? = nil
|
private var clientKey: URL? = nil
|
||||||
private var inboxPrefix: String = "_INBOX."
|
private var inboxPrefix: String = "_INBOX."
|
||||||
|
private var clientName: String = ""
|
||||||
|
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
|
/// Sets the client name sent to the server during CONNECT.
|
||||||
|
/// This name appears in system events and can be used to identify the client.
|
||||||
|
public func name(_ name: String) -> NatsClientOptions {
|
||||||
|
self.clientName = name
|
||||||
|
return self
|
||||||
|
}
|
||||||
|
|
||||||
/// Sets the prefix for inbox subjects used for request/reply.
|
/// Sets the prefix for inbox subjects used for request/reply.
|
||||||
/// Defaults to "_INBOX."
|
/// Defaults to "_INBOX."
|
||||||
@@ -195,7 +203,8 @@ public class NatsClientOptions {
|
|||||||
clientCertificate: clientCertificate,
|
clientCertificate: clientCertificate,
|
||||||
clientKey: clientKey,
|
clientKey: clientKey,
|
||||||
rootCertificate: rootCertificate,
|
rootCertificate: rootCertificate,
|
||||||
retryOnFailedConnect: initialReconnect
|
retryOnFailedConnect: initialReconnect,
|
||||||
|
name: clientName
|
||||||
)
|
)
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,6 +46,7 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
private var rootCertificate: URL?
|
private var rootCertificate: URL?
|
||||||
private var clientCertificate: URL?
|
private var clientCertificate: URL?
|
||||||
private var clientKey: URL?
|
private var clientKey: URL?
|
||||||
|
private let clientName: String
|
||||||
|
|
||||||
typealias InboundIn = ByteBuffer
|
typealias InboundIn = ByteBuffer
|
||||||
private let state = NIOLockedValueBox(NatsState.pending)
|
private let state = NIOLockedValueBox(NatsState.pending)
|
||||||
@@ -86,7 +87,7 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
retainServersOrder: Bool,
|
retainServersOrder: Bool,
|
||||||
pingInterval: TimeInterval, auth: Auth?, requireTls: Bool, tlsFirst: Bool,
|
pingInterval: TimeInterval, auth: Auth?, requireTls: Bool, tlsFirst: Bool,
|
||||||
clientCertificate: URL?, clientKey: URL?,
|
clientCertificate: URL?, clientKey: URL?,
|
||||||
rootCertificate: URL?, retryOnFailedConnect: Bool
|
rootCertificate: URL?, retryOnFailedConnect: Bool, name: String
|
||||||
) {
|
) {
|
||||||
self.urls = urls
|
self.urls = urls
|
||||||
self.group = .singleton
|
self.group = .singleton
|
||||||
@@ -102,6 +103,7 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
self.clientKey = clientKey
|
self.clientKey = clientKey
|
||||||
self.rootCertificate = rootCertificate
|
self.rootCertificate = rootCertificate
|
||||||
self.retryOnFailedConnect = retryOnFailedConnect
|
self.retryOnFailedConnect = retryOnFailedConnect
|
||||||
|
self.clientName = name
|
||||||
}
|
}
|
||||||
|
|
||||||
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
|
||||||
@@ -485,7 +487,7 @@ class ConnectionHandler: ChannelInboundHandler {
|
|||||||
|
|
||||||
private func sendClientConnectInit() async throws {
|
private func sendClientConnectInit() async throws {
|
||||||
var initialConnect = ConnectInfo(
|
var initialConnect = ConnectInfo(
|
||||||
verbose: false, pedantic: false, userJwt: nil, nkey: "", name: "", echo: true,
|
verbose: false, pedantic: false, userJwt: nil, nkey: "", name: self.clientName, echo: true,
|
||||||
lang: self.lang, version: self.version, natsProtocol: .dynamic, tlsRequired: false,
|
lang: self.lang, version: self.version, natsProtocol: .dynamic, tlsRequired: false,
|
||||||
user: self.auth?.user ?? "", pass: self.auth?.password ?? "",
|
user: self.auth?.user ?? "", pass: self.auth?.password ?? "",
|
||||||
authToken: self.auth?.token ?? "", headers: true, noResponders: true)
|
authToken: self.auth?.token ?? "", headers: true, noResponders: true)
|
||||||
|
|||||||
Reference in New Issue
Block a user