gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/server/context.go (about)

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  )
     7  
     8  type serverKey struct{}
     9  
    10  func wait(ctx context.Context) *sync.WaitGroup {
    11  	if ctx == nil {
    12  		return nil
    13  	}
    14  	wg, ok := ctx.Value("wait").(*sync.WaitGroup)
    15  	if !ok {
    16  		return nil
    17  	}
    18  	return wg
    19  }
    20  
    21  func FromContext(ctx context.Context) (Server, bool) {
    22  	c, ok := ctx.Value(serverKey{}).(Server)
    23  	return c, ok
    24  }
    25  
    26  func NewContext(ctx context.Context, s Server) context.Context {
    27  	return context.WithValue(ctx, serverKey{}, s)
    28  }