tokers commented on a change in pull request #770: URL: https://github.com/apache/apisix-ingress-controller/pull/770#discussion_r757186005
########## File path: pkg/api/router/apisix_healthz.go ########## @@ -0,0 +1,49 @@ +// 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. + +package router + +import ( + "net/http" + "sync" + + "github.com/gin-gonic/gin" +) + +// HealthState stores healthcheck err of APISIX +type HealthState struct { + sync.RWMutex + + Err error +} + +// MountWebhooks mounts apisix healthz route. +func MountApisixHealthz(r *gin.Engine, state *HealthState) { + r.GET("/apisix/healthz", apisixHealthz(state)) +} + +func apisixHealthz(state *HealthState) gin.HandlerFunc { + return func(c *gin.Context) { + state.RLock() + defer state.RUnlock() Review comment: I think here to unlock the mutex in the `defer` is not so proper since we have some I/O operations (maybe the goroutine will be blocked for a long while since the TCP flow/congestion control. Let's avoid these situations. ########## File path: pkg/ingress/controller.go ########## @@ -653,6 +653,10 @@ func (c *Controller) checkClusterHealth(ctx context.Context, cancelFunc context. if err != nil { // Finally failed health check, then give up leader. log.Warnf("failed to check health for default cluster: %s, give up leader", err) + c.apiServer.HealthState.Lock() + defer c.apiServer.HealthState.Unlock() + + c.apiServer.HealthState.Err = err Review comment: We may put the health state to the `cluster`? So that if we have multiple APISIX clusters (in the future), we don't have to change the existing codes. ########## File path: pkg/api/router/apisix_healthz.go ########## @@ -0,0 +1,49 @@ +// 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. + +package router + +import ( + "net/http" + "sync" + + "github.com/gin-gonic/gin" +) + +// HealthState stores healthcheck err of APISIX +type HealthState struct { Review comment: Let's put it to the `types.go`. -- 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: notifications-unsubscr...@apisix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org