This is an automated email from the ASF dual-hosted git repository.

ashishtiwari pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix-ingress-controller.git


The following commit(s) were added to refs/heads/master by this push:
     new e6e58724 refactor(cmd/ingress): invert signal ctx logic (#2139)
e6e58724 is described below

commit e6e58724ea9da3514254e3d020b562d5031ef94a
Author: Aurelia <[email protected]>
AuthorDate: Wed Jan 31 18:12:45 2024 +0100

    refactor(cmd/ingress): invert signal ctx logic (#2139)
    
    * refactor(cmd/ingress): invert signal ctx logic
    
    this commit changes the signal handling in cmd/ingress to be wrapped in
    a context, and inverts which goroutine runs the controller and
    which watches for the context to be cancelled, which allows some
    scaffolding (`sync.WaitGroup`) to be removed and now properly handles
    the controller exiting with `nil` (as it does when leader election
    fails)
---
 cmd/ingress/ingress.go      | 42 ++++++++++++++++++++----------------------
 pkg/providers/controller.go |  9 +++------
 2 files changed, 23 insertions(+), 28 deletions(-)

diff --git a/cmd/ingress/ingress.go b/cmd/ingress/ingress.go
index 3d2311e8..34bb05e7 100644
--- a/cmd/ingress/ingress.go
+++ b/cmd/ingress/ingress.go
@@ -15,12 +15,12 @@
 package ingress
 
 import (
+       "context"
        "encoding/json"
        "fmt"
        "os"
        "os/signal"
        "strings"
-       "sync"
        "syscall"
        "time"
 
@@ -42,13 +42,19 @@ func dief(template string, args ...interface{}) {
        os.Exit(1)
 }
 
-func waitForSignal(stopCh chan struct{}) {
-       sigCh := make(chan os.Signal, 1)
-       signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
-
-       sig := <-sigCh
-       log.Infof("signal %d (%s) received", sig, sig.String())
-       close(stopCh)
+func contextWithSignalCancel(ctx context.Context, signals ...os.Signal) 
context.Context {
+       newCtx, cancel := context.WithCancel(ctx)
+       go func() {
+               sigCh := make(chan os.Signal, 1)
+               signal.Notify(sigCh, signals...)
+
+               sig := <-sigCh
+               log.Infof("signal %d (%s) received", sig, sig.String())
+               signal.Stop(sigCh)
+               close(sigCh)
+               cancel()
+       }()
+       return newCtx
 }
 
 // NewIngressCommand creates the ingress sub command for 
apisix-ingress-controller.
@@ -118,8 +124,8 @@ the apisix cluster and others are created`,
                                dief("failed to initialize logging: %s", err)
                        }
                        log.DefaultLogger = logger
-                       log.Info("init apisix ingress controller")
 
+                       log.Info("init apisix ingress controller")
                        log.Info("version:\n", version.Long())
 
                        // We should make sure that the cfg that's logged out 
is sanitized.
@@ -132,25 +138,17 @@ the apisix cluster and others are created`,
                        }
                        log.Info("use configuration\n", string(data))
 
-                       stop := make(chan struct{})
+                       ctx := contextWithSignalCancel(context.Background(), 
syscall.SIGINT, syscall.SIGTERM)
+
                        ingress, err := controller.NewController(cfg)
                        if err != nil {
                                dief("failed to create ingress controller: %s", 
err)
                        }
-                       wg := sync.WaitGroup{}
-                       wg.Add(1)
-                       go func() {
-                               defer wg.Done()
 
-                               log.Info("start ingress controller")
-
-                               if err := ingress.Run(stop); err != nil {
-                                       dief("failed to run ingress controller: 
%s", err)
-                               }
-                       }()
+                       if err := ingress.Run(ctx); err != nil {
+                               dief("failed to run ingress controller: %s", 
err)
+                       }
 
-                       waitForSignal(stop)
-                       wg.Wait()
                        log.Info("apisix ingress controller exited")
                },
        }
diff --git a/pkg/providers/controller.go b/pkg/providers/controller.go
index a2ebc253..d8693221 100644
--- a/pkg/providers/controller.go
+++ b/pkg/providers/controller.go
@@ -144,13 +144,10 @@ func (c *Controller) Eventf(_ runtime.Object, eventType 
string, reason string, m
 }
 
 // Run launches the controller.
-func (c *Controller) Run(stop chan struct{}) error {
-       rootCtx, rootCancel := context.WithCancel(context.Background())
+func (c *Controller) Run(ctx context.Context) error {
+       rootCtx, rootCancel := context.WithCancel(ctx)
        defer rootCancel()
-       go func() {
-               <-stop
-               rootCancel()
-       }()
+
        c.MetricsCollector.ResetLeader(false)
 
        go func() {

Reply via email to