add close

This commit is contained in:
yanghao05
2023-06-01 11:02:10 +08:00
parent 690df80c39
commit d28d9649ed
6 changed files with 20 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ import (
var Container *dig.Container = dig.New()
var Cancel context.CancelFunc
var closeable []func()
func init() {
if err := Container.Provide(func() context.Context {
@@ -20,6 +21,18 @@ func init() {
}); err != nil {
log.Fatal(err)
}
closeable = make([]func(), 0)
}
func AddCloseAble(c func()) {
closeable = append(closeable, c)
}
func Close() {
for _, c := range closeable {
c()
}
}
type ProviderContainer struct {

1
go.mod
View File

@@ -7,7 +7,6 @@ require (
github.com/gin-gonic/gin v1.8.2
github.com/glebarez/sqlite v1.5.0
github.com/go-gormigrate/gormigrate/v2 v2.0.2
github.com/go-redis/redis/v8 v8.11.5
github.com/gofrs/uuid v4.0.0+incompatible
github.com/golang-jwt/jwt/v4 v4.4.3
github.com/imroc/req/v3 v3.34.0

3
go.sum
View File

@@ -159,8 +159,6 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
@@ -424,7 +422,6 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
github.com/onsi/ginkgo/v2 v2.9.2 h1:BA2GMJOtfGAfagzYtrAlufIP0lq6QERkFmHLMLPwFSU=
github.com/onsi/ginkgo/v2 v2.9.2/go.mod h1:WHcJJG2dIlcCqVfBAwUCrJxSPFb6v4azBwgxeMeDuts=
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=

View File

@@ -24,6 +24,9 @@ func Provide(opts ...opt.Option) error {
return nil, errors.Wrap(err, "failed to ping")
}
container.AddCloseAble(func() {
client.Close()
})
return client, nil
}, o.DiOptions()...)
}

View File

@@ -14,6 +14,8 @@ type GrpcService struct {
}
func ServeGrpc() error {
defer container.Close()
return container.Container.Invoke(func(grpc GrpcService) error {
for _, svc := range grpc.Services {
grpc.Server.RegisterService(svc.Name(), svc.Register)

View File

@@ -14,6 +14,8 @@ type Http struct {
}
func ServeHttp() error {
defer container.Close()
return container.Container.Invoke(func(http Http) error {
for _, route := range http.Routes {
route.Register()