/shutdown

Gracefully shutdown using context.Context and sync.GroupWait

Primary LanguageGoMIT LicenseMIT

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
	}