Skip to content

Commit ccf3387

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

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

session.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,87 @@ 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+
defer iter.Close()
824+
825+
var connections []*ClientConnection
826+
827+
for {
828+
conn := &ClientConnection{}
829+
830+
// Variables to hold nullable fields
831+
var keyspaceName, sslCipherSuite, sslProtocol *string
832+
833+
if !iter.Scan(
834+
&conn.Address,
835+
&conn.Port,
836+
&conn.ConnectionStage,
837+
&conn.DriverName,
838+
&conn.DriverVersion,
839+
&conn.Hostname,
840+
&keyspaceName,
841+
&conn.ProtocolVersion,
842+
&conn.RequestCount,
843+
&sslCipherSuite,
844+
&conn.SSLEnabled,
845+
&sslProtocol,
846+
&conn.Username,
847+
) {
848+
if err := iter.Close(); err != nil {
849+
return nil, err
850+
}
851+
break
852+
}
853+
854+
conn.KeyspaceName = keyspaceName
855+
conn.SSLCipherSuite = sslCipherSuite
856+
conn.SSLProtocol = sslProtocol
857+
858+
connections = append(connections, conn)
859+
}
860+
861+
return connections, nil
862+
}
863+
783864
type hostMetrics struct {
784865
// Attempts is count of how many times this query has been attempted for this host.
785866
// An attempt is either a retry or fetching next page of results.

0 commit comments

Comments
 (0)