trpc.group/trpc-go/trpc-go@v1.0.3/server/serve_windows.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  //go:build windows
    15  // +build windows
    16  
    17  package server
    18  
    19  import (
    20  	"os"
    21  	"os/signal"
    22  	"sync"
    23  	"syscall"
    24  	"time"
    25  
    26  	"github.com/hashicorp/go-multierror"
    27  	"trpc.group/trpc-go/trpc-go/log"
    28  )
    29  
    30  // Serve implements Service, starting all services that belong to this server
    31  func (s *Server) Serve() error {
    32  	defer log.Sync()
    33  	if len(s.services) == 0 {
    34  		panic("service empty")
    35  	}
    36  	s.signalCh = make(chan os.Signal)
    37  	s.closeCh = make(chan struct{})
    38  
    39  	var (
    40  		mu     sync.Mutex
    41  		svrErr error
    42  	)
    43  	for name, service := range s.services {
    44  		go func(name string, service Service) {
    45  			if err := service.Serve(); err != nil {
    46  				mu.Lock()
    47  				svrErr = multierror.Append(svrErr, err).ErrorOrNil()
    48  				mu.Unlock()
    49  				s.failedServices.Store(name, service)
    50  				time.Sleep(time.Millisecond * 300)
    51  				s.signalCh <- syscall.SIGTERM
    52  			}
    53  		}(name, service)
    54  	}
    55  
    56  	signal.Notify(s.signalCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGSEGV)
    57  	select {
    58  	case <-s.closeCh:
    59  	case <-s.signalCh:
    60  	}
    61  
    62  	s.tryClose()
    63  	if svrErr != nil {
    64  		log.Errorf("service serve errors: %+v", svrErr)
    65  	}
    66  	return svrErr
    67  }