@@ -780,6 +780,95 @@ func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{})
780780 return applied , iter , iter .err
781781}
782782
783+ // connectionType is a custom type that represents the different stages
784+ // of a client connection in a Cassandra cluster. It is used to filter and categorize
785+ // connections based on their current state.
786+ type connectionType string
787+
788+ const (
789+ Ready connectionType = "ready"
790+ Connecting connectionType = "connecting"
791+ Idle connectionType = "idle"
792+ Closed connectionType = "closed"
793+ Failed connectionType = "failed"
794+ )
795+
796+ // ClientConnection represents a client connection to a Cassandra node. It holds detailed
797+ // information about the connection, including the client address, connection stage, driver details,
798+ // and various configuration options.
799+ type ClientConnection struct {
800+ Address string
801+ Port int
802+ ConnectionStage string
803+ DriverName string
804+ DriverVersion string
805+ Hostname string
806+ KeyspaceName * string
807+ ProtocolVersion int
808+ RequestCount int
809+ SSLCipherSuite * string
810+ SSLEnabled bool
811+ SSLProtocol * string
812+ Username string
813+ }
814+
815+ // RetrieveClientConnections retrieves a list of client connections from the
816+ // `system_views.clients` table based on the specified connection type. The function
817+ // queries the Cassandra database for connections with a given `connection_stage` and
818+ // scans the results into a slice of `ClientConnection` structs. It handles nullable
819+ // fields and returns the list of connections or an error if the operation fails.
820+ func (s * Session ) RetrieveClientConnections (connectionType connectionType ) ([]* ClientConnection , error ) {
821+ const stmt = `
822+ SELECT address, port, connection_stage, driver_name, driver_version,
823+ hostname, keyspace_name, protocol_version, request_count,
824+ ssl_cipher_suite, ssl_enabled, ssl_protocol, username
825+ FROM system_views.clients
826+ WHERE connection_stage = ?`
827+
828+ iter := s .control .query (stmt , connectionType )
829+ if iter .NumRows () == 0 {
830+ return nil , ErrKeyspaceDoesNotExist
831+ }
832+ defer iter .Close ()
833+
834+ var connections []* ClientConnection
835+ for {
836+ conn := & ClientConnection {}
837+
838+ // Variables to hold nullable fields
839+ var keyspaceName , sslCipherSuite , sslProtocol * string
840+
841+ if ! iter .Scan (
842+ & conn .Address ,
843+ & conn .Port ,
844+ & conn .ConnectionStage ,
845+ & conn .DriverName ,
846+ & conn .DriverVersion ,
847+ & conn .Hostname ,
848+ & keyspaceName ,
849+ & conn .ProtocolVersion ,
850+ & conn .RequestCount ,
851+ & sslCipherSuite ,
852+ & conn .SSLEnabled ,
853+ & sslProtocol ,
854+ & conn .Username ,
855+ ) {
856+ if err := iter .Close (); err != nil {
857+ return nil , err
858+ }
859+ break
860+ }
861+
862+ conn .KeyspaceName = keyspaceName
863+ conn .SSLCipherSuite = sslCipherSuite
864+ conn .SSLProtocol = sslProtocol
865+
866+ connections = append (connections , conn )
867+ }
868+
869+ return connections , nil
870+ }
871+
783872type hostMetrics struct {
784873 // Attempts is count of how many times this query has been attempted for this host.
785874 // An attempt is either a retry or fetching next page of results.
0 commit comments