martin-sucha commented on code in PR #1820:
URL: 
https://github.com/apache/cassandra-gocql-driver/pull/1820#discussion_r1758548535


##########
example_interceptor_test.go:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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"
+       "fmt"
+       "log"
+       "time"
+
+       gocql "github.com/gocql/gocql"
+)
+
+type MyQueryAttemptInterceptor struct {
+       injectFault bool
+}
+
+func (q MyQueryAttemptInterceptor) Intercept(
+       ctx context.Context,
+       query gocql.ExecutableQuery,
+       conn *gocql.Conn,
+       handler gocql.QueryAttemptHandler,
+) *gocql.Iter {
+       switch q := query.(type) {
+       case *gocql.Query:
+               // Inspect or modify query
+               query = q
+       case *gocql.Batch:
+               // Inspect or modify batch
+               query = q
+       }
+
+       // 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 && query.Attempts() == 0 {

Review Comment:
   Should the attempt number be a parameter to `Intercept`? It seems that 
calling `Attempts()` could be racy, especially when speculative execution is 
enabled:
   
   1. Attempt 0 started
   2. Attempt 1 started
   3. Interceptor 0 called - gets attempt 0
   4. Interceptor 1 called - gets attempt 0
   5. attempts incremented
   6. attempts incremented
   
   Perhaps the signature should be `Intercept(ctx context.Context, attempt 
gocql.QueryAttempt, handler gocql.QueryAttemptHandler)`? That would enable 
adding more information about the attempt in the future.
   
   ```go
   type QueryAttempt struct {
        // query to execute
        query gocql.ExecutableQuery
        // conn to use to execute the query
        conn *gocql.Conn
        // number of the attempt. 0 is the initial attempt, 1 is first retry, 
etc.
        number int
   }
   ```
   
   ```go
   type QueryAttemptHandler = func(context.Context, QueryAttempt) *Iter
   ```



-- 
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]

Reply via email to