github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/common/context/context.go (about)

     1  package context
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  	"time"
     7  )
     8  
     9  type remoteAddr struct{}
    10  type localAddr struct{}
    11  type initData struct{}
    12  type InitData struct {
    13  	Start       time.Time
    14  	ServiceName string
    15  	MsgType     uint8
    16  }
    17  
    18  func WithRemoteAddr(ctx context.Context, addr net.Addr) context.Context {
    19  	return context.WithValue(ctx, remoteAddr{}, addr)
    20  }
    21  
    22  func CheckRemoteAddr(ctx context.Context) net.Addr {
    23  	a, _ := ctx.Value(remoteAddr{}).(net.Addr)
    24  	return a
    25  }
    26  
    27  func WithLocalAddr(ctx context.Context, addr net.Addr) context.Context {
    28  	return context.WithValue(ctx, localAddr{}, addr)
    29  }
    30  
    31  func CheckLocalAddr(ctx context.Context) net.Addr {
    32  	a, _ := ctx.Value(localAddr{}).(net.Addr)
    33  	return a
    34  }
    35  
    36  func WithInitData(ctx context.Context, p *InitData) context.Context {
    37  	return context.WithValue(ctx, initData{}, p)
    38  }
    39  
    40  func CheckInitData(ctx context.Context) *InitData {
    41  	a, _ := ctx.Value(initData{}).(*InitData)
    42  	return a
    43  }