tokers commented on a change in pull request #453:
URL: 
https://github.com/apache/apisix-ingress-controller/pull/453#discussion_r634153781



##########
File path: pkg/apisix/cluster.go
##########
@@ -290,6 +310,51 @@ func (c *cluster) GlobalRule() GlobalRule {
        return c.globalRules
 }
 
+// HealthCheck implements Cluster.HealthCheck method.
+func (c *cluster) HealthCheck(ctx context.Context) (err error) {
+       if c.cacheSyncErr != nil {
+               err = c.cacheSyncErr
+               return
+       }
+       if atomic.LoadInt32(&c.cacheState) == _cacheSyncing {
+               return
+       }
+
+       // Retry three times in a row, and exit if all of them fail.
+       backoff := wait.Backoff{
+               Duration: 5 * time.Second,
+               Factor:   1,
+               Steps:    3,
+       }
+       var lastCheckErr error
+       err = wait.ExponentialBackoffWithContext(ctx, backoff, func() (done 
bool, _ error) {
+               if lastCheckErr = c.healthCheck(ctx); lastCheckErr != nil {
+                       log.Warnf("failed to check health for cluster %s: %s, 
will retry", c.name, lastCheckErr)
+                       return
+               }
+               done = true
+               return
+       })
+       if err != nil {
+               // if ErrWaitTimeout then set lastSyncErr
+               c.cacheSyncErr = lastCheckErr
+       }
+       return err
+}
+
+func (c *cluster) healthCheck(ctx context.Context) (err error) {
+       // tcp socket probe
+       d := net.Dialer{Timeout: 3 * time.Second}
+       conn, err := d.DialContext(ctx, "tcp", c.baseURLHost)
+       if err != nil {
+               return err
+       }
+       if er := conn.Close(); er != nil {
+               log.Warnf("failed close tcp socket: %s", er)

Review comment:
       ```suggestion
                log.Warnw("failed to close tcp probe connection",
                        zap.Error(err),
                        zap.String("cluster", c.name),
                )
   ```

##########
File path: pkg/apisix/cluster.go
##########
@@ -63,6 +77,7 @@ type ClusterOptions struct {
 type cluster struct {
        name         string
        baseURL      string
+       baseURLHost  string

Review comment:
       If host is required, we can change the `baseURL` to `pathPrefix`, 
currently, `baseURLHost` is partial of `baseURL`.

##########
File path: pkg/apisix/cluster.go
##########
@@ -290,6 +310,51 @@ func (c *cluster) GlobalRule() GlobalRule {
        return c.globalRules
 }
 
+// HealthCheck implements Cluster.HealthCheck method.
+func (c *cluster) HealthCheck(ctx context.Context) (err error) {
+       if c.cacheSyncErr != nil {

Review comment:
       Actually, I think health check should be independent with the first time 
cache sync.

##########
File path: pkg/ingress/controller.go
##########
@@ -317,30 +325,47 @@ election:
 }
 
 func (c *Controller) run(ctx context.Context) {
-       log.Infow("controller now is running as leader",
+       log.Infow("controller tries to leading ...",
                zap.String("namespace", c.namespace),
                zap.String("pod", c.name),
        )
+
+       var cancelFunc context.CancelFunc

Review comment:
       It seems that you don't merge from the latest master, here we already 
have some methods to finalize the context passed to `run`.
   
   Here we cannot cancel the context of `ctx` in parameters, so we cannot give 
up the leader role by ourselves.

##########
File path: pkg/apisix/cluster.go
##########
@@ -63,6 +77,7 @@ type ClusterOptions struct {
 type cluster struct {
        name         string
        baseURL      string
+       baseURLHost  string

Review comment:
       If host is required, we can change the `baseURL` to `pathPrefix`, 
currently, `baseURLHost` is partial of `baseURL`.

##########
File path: pkg/apisix/cluster.go
##########
@@ -50,6 +51,19 @@ var (
        ErrDuplicatedCluster = errors.New("duplicated cluster")
 
        _errReadOnClosedResBody = errors.New("http: read on closed response 
body")
+
+       // Default shared transport if apisix client

Review comment:
       ```suggestion
        // Default shared transport for apisix client
   ```
   
   We may export options to customize this transport in the future (in command 
line options).

##########
File path: pkg/ingress/controller.go
##########
@@ -423,3 +455,21 @@ func (c *Controller) syncSSL(ctx context.Context, ssl 
*apisixv1.Ssl, event types
        }
        return err
 }
+
+func (c *Controller) checkClusterHealth(ctx context.Context, cancelFunc 
context.CancelFunc) {
+       defer cancelFunc()
+       for {
+               select {
+               case <-ctx.Done():

Review comment:
       Just return in this arm.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to