github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/shutdown/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"time"
     6  
     7  	"context"
     8  	"github.com/micro/go-micro/v2"
     9  )
    10  
    11  func main() {
    12  	// cancellation context
    13  	ctx, cancel := context.WithCancel(context.Background())
    14  
    15  	// shutdown after 5 seconds
    16  	go func() {
    17  		<-time.After(time.Second * 5)
    18  		log.Println("Shutdown example: shutting down service")
    19  		cancel()
    20  	}()
    21  
    22  	// create service
    23  	service := micro.NewService(
    24  		// with our cancellation context
    25  		micro.Context(ctx),
    26  	)
    27  
    28  	// init service
    29  	service.Init()
    30  
    31  	// run service
    32  	service.Run()
    33  }