Skip to content

Gracefully shutdown using context.Context and sync.GroupWait

License

Notifications You must be signed in to change notification settings

zhangpeihao/shutdown

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gracefully shutdown using context.Context and sync.WaitGroup

GoDoc Go Report Card

Example

	// Generate a new context
	ctx := NewContext()

	// Run service with this context
	go func(ctx context.Context) {
		if err := ExitWaitGroupAdd(ctx, 1); err != nil {
			return
		}
		defer ExitWaitGroupDone(ctx)

		otherEvent := make(chan struct{})
	FOR_LOOP:
		for {
			select {
			case <-ctx.Done():
				break FOR_LOOP
			case <-otherEvent:
				// ...
			}
		}
		
		// Some close processes
	}(ctx)

	// Wait interrupt signal and shutdown gracefully
	if err := WaitAndShutdown(ctx, time.Second*5, func(timeout time.Duration) error {
		log.Println("close")
		return nil
	}); err != nil {
		log.Println("Shutdown error:", err)
		return
	}

About

Gracefully shutdown using context.Context and sync.GroupWait

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages