Repository: go-kratos/kratos
Pull request #1649 (2021-11-24 06:25:13)

feat(app): app info delivery to stop context
<!--
🎉 Thanks for sending a pull request to Kratos! Here are some tips for you:

1. If this is your first time contributing to Kratos, please read our contribution guide: https://go-kratos.dev/en/docs/community/contribution/
2. Ensure you have added or ran the appropriate tests and lint for your PR, please use `make lint` and `make test` before filing your PR, use `make clean` to tidy your go mod.
3. If the PR is unfinished, you may need mark it as a WIP(Work In Progress) PR or draft PR
4. Please use a conventional commits format title: `<type>[optional scope]: <description>`
    
    Some suggestion on <type>:

    fix: A bug fix
    feat: A new feature
    test: Adding missing tests or correcting existing tests
    refactor: A code change that neither fixes a bug nor adds a feature
    break: Changes has break change

    docs: Documentation only changes
    deps: Changes external dependencies
    style: Changes that do not affect the meaning of the code (white-space, formatting, etc)
    chore Daily work, examples, etc.
    ci: Changes to our CI configuration files and scripts
-->

#### Description (what this PR does / why we need it):
<!--
* The description should include the motivation for this PR or contrast this with previous behavior
-->


#### Which issue(s) this PR fixes (resolves / be part of):
<!--
* Automatically closes linked issue when PR is merged.
* If you PR is not fully resolved issue, please use `part of #<issue number>` instead.

Usage: `fixes/resolves #<issue number>`, or `fixes/resolves (paste link of issue)`.
-->
fixes #

#### Other special notes for reviewer:
<!--
* Somethings that need extra attention for reviewer
* Some additional notes, TODO list, etc.
-->
issue #1584 


Patch:
diff --git a/app.go b/app.go
index bfbb85e0..736c27a1 100644
--- a/app.go
+++ b/app.go
@@ -91,7 +91,7 @@ func (a *App) Run() error {
 		srv := srv
 		eg.Go(func() error {
 			<-ctx.Done() // wait for stop signal
-			sctx, cancel := context.WithTimeout(context.Background(), a.opts.stopTimeout)
+			sctx, cancel := context.WithTimeout(NewContext(context.Background(), a), a.opts.stopTimeout)
 			defer cancel()
 			return srv.Stop(sctx)
 		})
diff --git a/contrib/registry/nacos/watcher.go b/contrib/registry/nacos/watcher.go
index 3cb1d0f7..22961b69 100644
--- a/contrib/registry/nacos/watcher.go
+++ b/contrib/registry/nacos/watcher.go
@@ -18,7 +18,7 @@ type watcher struct {
 	groupName   string
 	ctx         context.Context
 	cancel      context.CancelFunc
-	watchChan   chan bool
+	watchChan   chan struct{}
 	cli         naming_client.INamingClient
 }
 
@@ -28,7 +28,7 @@ func newWatcher(ctx context.Context, cli naming_client.INamingClient, serviceNam
 		clusters:    clusters,
 		groupName:   groupName,
 		cli:         cli,
-		watchChan:   make(chan bool, 1),
+		watchChan:   make(chan struct{}, 1),
 	}
 	w.ctx, w.cancel = context.WithCancel(ctx)
 
@@ -37,7 +37,7 @@ func newWatcher(ctx context.Context, cli naming_client.INamingClient, serviceNam
 		Clusters:    clusters,
 		GroupName:   groupName,
 		SubscribeCallback: func(services []model.SubscribeService, err error) {
-			w.watchChan <- true
+			w.watchChan <- struct{}{}
 		},
 	})
 	return w, e
diff --git a/transport/grpc/resolver/discovery/builder.go b/transport/grpc/resolver/discovery/builder.go
index e6a56b53..de8dccf2 100644
--- a/transport/grpc/resolver/discovery/builder.go
+++ b/transport/grpc/resolver/discovery/builder.go
@@ -63,7 +63,7 @@ func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts res
 		err error
 		w   registry.Watcher
 	)
-	done := make(chan bool, 1)
+	done := make(chan struct{}, 1)
 	ctx, cancel := context.WithCancel(context.Background())
 	go func() {
 		w, err = b.discoverer.Watch(ctx, strings.TrimPrefix(target.URL.Path, "/"))


Test patch:
diff --git a/contrib/config/nacos/config_test.go b/contrib/config/nacos/config_test.go
index 9065ac91..2fdc4f6b 100644
--- a/contrib/config/nacos/config_test.go
+++ b/contrib/config/nacos/config_test.go
@@ -88,10 +88,10 @@ logger:
 	}
 	fmt.Println("get value", name)
 
-	done := make(chan bool)
+	done := make(chan struct{})
 	err = c.Watch("logger.level", func(key string, value kconfig.Value) {
 		fmt.Println(key, " value change", value)
-		done <- true
+		done <- struct{}{}
 	})
 	if err != nil {
 		t.Fatal(err)