AlexStocks commented on code in PR #421: URL: https://github.com/apache/dubbo-go-pixiu/pull/421#discussion_r873149149
########## pkg/cluster/healthcheck/healthcheck.go: ########## @@ -0,0 +1,282 @@ +/* + * 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 healthcheck + +import ( + "runtime/debug" + "sync/atomic" + "time" +) + +import ( + gxtime "github.com/dubbogo/gost/time" +) + +import ( + "github.com/apache/dubbo-go-pixiu/pkg/logger" + "github.com/apache/dubbo-go-pixiu/pkg/model" +) + +const ( + DefaultTimeout = time.Second + DefaultInterval = 3 * time.Second + + DefaultHealthyThreshold uint32 = 5 + DefaultUnhealthyThreshold uint32 = 5 + DefaultFirstInterval = 5 * time.Second +) + +type HealthChecker struct { + checkers map[string]*EndpointChecker + sessionConfig map[string]interface{} + // check config + timeout time.Duration + intervalBase time.Duration + healthyThreshold uint32 + initialDelay time.Duration + cluster *model.ClusterConfig + unhealthyThreshold uint32 +} + +// EndpointChecker is a wrapper of types.HealthCheckSession for health check +type EndpointChecker struct { + endpoint *model.Endpoint + HealthChecker *HealthChecker + // TCP checker, can extend to http, grpc, dubbo or other protocol checker + tcpChecker *TCPChecker + resp chan checkResponse + timeout chan bool + checkID uint64 + stop chan struct{} + checkTimer *gxtime.Timer + checkTimeout *gxtime.Timer + unHealthCount uint32 + healthCount uint32 + threshold uint32 +} + +type checkResponse struct { + ID uint64 + Healthy bool +} + +func CreateHealthCheck(cluster *model.ClusterConfig, cfg model.HealthCheckConfig) *HealthChecker { + + timeout, err := time.ParseDuration(cfg.TimeoutConfig) + if err != nil { + logger.Infof("[health check] timeout parse duration error %s", cfg.TimeoutConfig) + timeout = DefaultTimeout + } + + interval, err := time.ParseDuration(cfg.IntervalConfig) + if err != nil { + logger.Infof("[health check] interval parse duration error %s", cfg.TimeoutConfig) + interval = DefaultInterval + } + + initialDelay, err := time.ParseDuration(cfg.IntervalConfig) + if err != nil { + logger.Infof("[health check] initialDelay parse duration error %s", cfg.TimeoutConfig) Review Comment: 同上 -- 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]
