worryg0d commented on code in PR #1943: URL: https://github.com/apache/cassandra-gocql-driver/pull/1943#discussion_r3077686013
########## example_interceptor_test.go: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Content before git sha 34fdeebefcbf183ed7f916f931aa0586fdaa1b40 + * Copyright (c) 2016, The Gocql authors, + * provided under the BSD-3-Clause License. + * See the NOTICE file distributed with this work for additional information. + */ + +package gocql_test + +import ( + "context" + "log" + "time" + + gocql "github.com/apache/cassandra-gocql-driver/v2" +) + +type MyQueryAttemptInterceptor struct { + injectFault bool +} + +func (q MyQueryAttemptInterceptor) Intercept( + ctx context.Context, + attempt gocql.QueryAttempt, + handler gocql.QueryAttemptHandler, +) (*gocql.Iter, error) { + switch q := attempt.Statement.Statement().(type) { + case *gocql.Query: + // Inspect query + log.Println(q.Statement()) + case *gocql.Batch: + // Inspect batch + + log.Println(q.Entries[0].Stmt) + } + + // Inspect or modify context + ctx = context.WithValue(ctx, "trace-id", "123") + + // Optionally bypass the handler and return an error to prevent query execution. + // For example, to simulate query timeouts. + if q.injectFault && attempt.Attempts == 0 { + <-time.After(1 * time.Second) + return nil, gocql.RequestErrWriteTimeout{} + } + + // The interceptor *must* invoke the handler to execute the query. + return handler(ctx) +} + +// Example_interceptor demonstrates how to implement a QueryAttemptInterceptor. +func Example_interceptor() { + cluster := gocql.NewCluster("localhost:9042") + cluster.QueryAttemptInterceptor = MyQueryAttemptInterceptor{injectFault: true} + + session, err := cluster.CreateSession() + if err != nil { + log.Fatal(err) + } + defer session.Close() + + ctx := context.Background() + + var stringValue string + err = session.Query("select now() from system.local"). + RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 2}). + ScanContext(ctx, &stringValue) + if err != nil { + log.Fatalf("query failed %T", err) + } +} + +type QueryAttemptInterceptorChain struct { Review Comment: I think we can make this a part of gocql api - we already expose a couple of similar types for event listeners: `SessionReadyListenersMux `, `SchemaListenersMux`, etc. ########## cluster.go: ########## @@ -258,6 +258,10 @@ type ClusterConfig struct { // See https://issues.apache.org/jira/browse/CASSANDRA-10786 DisableSkipMetadata bool + // QueryAttemptInterceptor will set the provided query interceptor on all queries created from this session. + // Use it to intercept and modify queries by providing an implementation of QueryAttemptInterceptor. + QueryAttemptInterceptor QueryAttemptInterceptor Review Comment: nit: The **Query** prefix might create a wrong impression that this specific option is related to `gocql.Query` request types only. I'm unsure which name is actually better here, but we can mention both `gocql.Batch` and `gocql.Query` in the doc at least. ########## example_interceptor_test.go: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Content before git sha 34fdeebefcbf183ed7f916f931aa0586fdaa1b40 + * Copyright (c) 2016, The Gocql authors, + * provided under the BSD-3-Clause License. + * See the NOTICE file distributed with this work for additional information. + */ + +package gocql_test + +import ( + "context" + "log" + "time" + + gocql "github.com/apache/cassandra-gocql-driver/v2" +) + +type MyQueryAttemptInterceptor struct { + injectFault bool +} + +func (q MyQueryAttemptInterceptor) Intercept( + ctx context.Context, + attempt gocql.QueryAttempt, + handler gocql.QueryAttemptHandler, +) (*gocql.Iter, error) { + switch q := attempt.Statement.Statement().(type) { + case *gocql.Query: + // Inspect query + log.Println(q.Statement()) + case *gocql.Batch: + // Inspect batch + + log.Println(q.Entries[0].Stmt) + } Review Comment: Basically, this allows in-place modification of the submitted Query / Batch, and it violates the immutable nature that the driver follows, so changes made by interceptors might make Query / Batch objects potentially unreusable. Is there any particular use case when you want to use the ability of interceptors to modify queries over their configuration API? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

