worryg0d commented on code in PR #1822:
URL: 
https://github.com/apache/cassandra-gocql-driver/pull/1822#discussion_r1803313311


##########
conn.go:
##########
@@ -474,8 +483,12 @@ func (s *startupCoordinator) startup(ctx context.Context, 
supported map[string][
        case error:
                return v
        case *readyFrame:
+               // Connection is successfully set up and ready to use Native 
Protocol v5
+               s.conn.startupCompleted = true

Review Comment:
   `startupCoordinator.setupConn` is complex, so I'll add its code to this 
message for clarification
   ```go
   func (s *startupCoordinator) setupConn(ctx context.Context) error {
        var cancel context.CancelFunc
        if s.conn.timeout > 0 {
                ctx, cancel = context.WithTimeout(ctx, s.conn.timeout)
        } else {
                ctx, cancel = context.WithCancel(ctx)
        }
        defer cancel()
   
        startupErr := make(chan error)
        go func() { // the reading goroutine
                for range s.frameTicker {
                        err := s.conn.recv(ctx)
                        if err != nil {
                                select {
                                case startupErr <- err:
                                case <-ctx.Done():
                                }
   
                                return
                        }
                }
        }()
   
        go func() { // the writing one
                defer close(s.frameTicker)
                err := s.options(ctx)
                select {
                case startupErr <- err:
                case <-ctx.Done():
                }
        }()
   
        select {
        case err := <-startupErr:
                if err != nil {
                        return err
                }
        case <-ctx.Done():
                return errors.New("gocql: no response to connection startup 
within timeout")
        }
   
        return nil
   }
   ```
   
   It runs two goroutines: the first listening for response from C* and the 
second for processing startup.
   The first gorounite starts listening data from C* when the 
`startupCoordinator.frameTicker` chan is not empty.
   The second goroutine sends queries to C* via 
[startupCoordinator.write](https://github.com/apache/cassandra-gocql-driver/blob/953e0df999cabb3f5eef714df9921c00e9f632c2/conn.go#L417)
 which also writes to `startupCoordinator.frameTicker` chan. So, the reading 
from C* only starts when the **writing gouroutine** sends a query to the C*, 
the both goroutines are synchronized by `startupCoordinator.frameTicker`. Once 
the startup is completed, those goroutines are finished.
   
   When `startupCoordinator.setupConn` is finished, `Conn.init` runs two new 
goroutines: one for connection serving, and another one for a heartbeat, so 
they both already have an updated state of `startupCompleted`.



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