From c64648344d2598a996c8632d67819b52237cb4a6 Mon Sep 17 00:00:00 2001 From: Nikita Skrynnik Date: Thu, 5 May 2022 15:52:04 +0700 Subject: [PATCH] apply comments Signed-off-by: Nikita Skrynnik --- go.mod | 3 ++ go.sum | 7 ++-- ...{liveness_checker.go => liveness_check.go} | 34 +++++++++---------- 3 files changed, 25 insertions(+), 19 deletions(-) rename pkg/kernel/tools/heal/{liveness_checker.go => liveness_check.go} (69%) diff --git a/go.mod b/go.mod index dd3c3d2f..429b52ef 100644 --- a/go.mod +++ b/go.mod @@ -16,9 +16,12 @@ require ( google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12 // indirect google.golang.org/grpc v1.42.0 ) +<<<<<<< HEAD <<<<<<< HEAD replace github.com/networkservicemesh/sdk => ../sdk ======= replace github.com/networkservicemesh/sdk => github.com/xzfc/networkservicemesh-sdk v0.0.0-20220414232223-3a19549f4efa >>>>>>> d826125 (Add IcmpLivenessChecker) +======= +>>>>>>> bd9afc5 (apply comments) diff --git a/go.sum b/go.sum index 6979b372..c559d094 100644 --- a/go.sum +++ b/go.sum @@ -204,7 +204,7 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/cobra v0.0.0-20181021141114-fe5e611709b0/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v0.0.0-20181024212040-082b515c9490/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= -github.com/spiffe/go-spiffe/v2 v2.0.0-alpha.4.0.20200528145730-dc11d0c74e85/go.mod h1:Z6jOEo3L49OpNaK5JTIOig6K9HJhwH6cb78MF5mothQ= +github.com/spiffe/go-spiffe/v2 v2.0.0/go.mod h1:TEfgrEcyFhuSuvqohJt6IxENUNeHfndWCCV1EX7UaVk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -391,21 +391,23 @@ google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12 h1:DN5b3HU13J4sMd/QjDx34U6afpaexKTDdop+26pdjdk= google.golang.org/genproto v0.0.0-20211129164237-f09f9a12af12/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34= google.golang.org/grpc v1.41.0/go.mod h1:U3l9uK9J0sini8mHphKoXyaqDA/8VyGnDee1zzIUK6k= google.golang.org/grpc v1.42.0 h1:XT2/MFpuPFsEX2fWh3YQtHkZ+WYZFQRfaUgLZYj/p6A= google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= +google.golang.org/grpc/examples v0.0.0-20201130180447-c456688b1860/go.mod h1:Ly7ZA/ARzg8fnPU9TyZIxoz33sEUuWX7txiqs8lPTgE= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= @@ -414,6 +416,7 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= diff --git a/pkg/kernel/tools/heal/liveness_checker.go b/pkg/kernel/tools/heal/liveness_check.go similarity index 69% rename from pkg/kernel/tools/heal/liveness_checker.go rename to pkg/kernel/tools/heal/liveness_check.go index cb0d0af2..a142f6aa 100644 --- a/pkg/kernel/tools/heal/liveness_checker.go +++ b/pkg/kernel/tools/heal/liveness_check.go @@ -20,26 +20,27 @@ package heal import ( "context" "net" -<<<<<<< HEAD "sync/atomic" -======= - "sync" ->>>>>>> d826125 (Add IcmpLivenessChecker) "time" "github.com/networkservicemesh/api/pkg/api/networkservice" - "github.com/networkservicemesh/sdk/pkg/networkservice/common/heal" + "github.com/networkservicemesh/sdk/pkg/tools/log" "github.com/tatsushid/go-fastping" ) -var _ heal.LivenessChecker = ICMPLivenessChecker +const ( + defaultTimeout = 200 * time.Millisecond +) -// ICMPLivenessChecker is an implementation of LivenessChecker. It sends ICMP -// pings continuously and waits for replies until the first missing reply or -// timeout. -func ICMPLivenessChecker(deadlineCtx context.Context, conn *networkservice.Connection) bool { - deadline, _ := deadlineCtx.Deadline() +// NewKernelLivenessCheck is an implementation of heal.LivenessCheck. It sends ICMP +// ping and checks reply. Returns false if didn't get reply. +func NewKernelLivenessCheck(deadlineCtx context.Context, conn *networkservice.Connection) bool { p := fastping.NewPinger() + deadline, ok := deadlineCtx.Deadline() + if !ok { + deadline = time.Now().Add(defaultTimeout) + } + p.MaxRTT = time.Until(deadline) addrCount := len(conn.GetContext().GetIpContext().GetDstIpAddrs()) @@ -58,19 +59,18 @@ func ICMPLivenessChecker(deadlineCtx context.Context, conn *networkservice.Conne atomic.AddInt32(&count, 1) } - alive := true + var aliveCh = make(chan bool) p.OnIdle = func() { - if int(count) != addrCount { - alive = false - return - } + aliveCh <- int(atomic.LoadInt32(&count)) == addrCount + close(aliveCh) } err := p.Run() if err != nil { + log.FromContext(deadlineCtx).Error("Ping failed: %s", err.Error()) return false } - return alive + return <-aliveCh }