Skip to content

Commit af5fe82

Browse files
committed
issue-1774: Function to retrieve info about client connections was added
1 parent 34fdeeb commit af5fe82

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

session.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,86 @@ 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 `json:"address"`
801+
Port int `json:"port"`
802+
ConnectionStage string `json:"connection_stage"`
803+
DriverName string `json:"driver_name"`
804+
DriverVersion string `json:"driver_version"`
805+
Hostname string `json:"hostname"`
806+
KeyspaceName *string `json:"keyspace_name"`
807+
ProtocolVersion int `json:"protocol_version"`
808+
RequestCount int `json:"request_count"`
809+
SSLCipherSuite *string `json:"ssl_cipher_suite"`
810+
SSLEnabled bool `json:"ssl_enabled"`
811+
SSLProtocol *string `json:"ssl_protocol"`
812+
Username string `json:"username"`
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+
query := "SELECT address, port, connection_stage, driver_name, driver_version, hostname, keyspace_name, protocol_version, request_count, ssl_cipher_suite, ssl_enabled, ssl_protocol, username FROM system_views.clients WHERE connection_stage = ?"
822+
iter := s.Query(query, connectionType).Iter()
823+
824+
var connections []*ClientConnection
825+
// Variables to hold nullable fields
826+
var keyspaceName, sslCipherSuite, sslProtocol *string
827+
828+
for {
829+
conn := &ClientConnection{}
830+
if !iter.Scan(
831+
&conn.Address,
832+
&conn.Port,
833+
&conn.ConnectionStage,
834+
&conn.DriverName,
835+
&conn.DriverVersion,
836+
&conn.Hostname,
837+
&keyspaceName,
838+
&conn.ProtocolVersion,
839+
&conn.RequestCount,
840+
&sslCipherSuite,
841+
&conn.SSLEnabled,
842+
&sslProtocol,
843+
&conn.Username,
844+
) {
845+
break
846+
}
847+
848+
conn.KeyspaceName = keyspaceName
849+
conn.SSLCipherSuite = sslCipherSuite
850+
conn.SSLProtocol = sslProtocol
851+
852+
connections = append(connections, conn)
853+
}
854+
855+
err := iter.Close()
856+
if err != nil {
857+
return connections, err
858+
}
859+
860+
return connections, nil
861+
}
862+
783863
type hostMetrics struct {
784864
// Attempts is count of how many times this query has been attempted for this host.
785865
// An attempt is either a retry or fetching next page of results.

0 commit comments

Comments
 (0)