github.com/wfusion/gofusion@v1.1.14/common/infra/watermill/components/metrics/ctx.go (about)

     1  package metrics
     2  
     3  import "context"
     4  
     5  type contextValue int
     6  
     7  const (
     8  	publishObserved contextValue = iota
     9  	subscribeObserved
    10  )
    11  
    12  // setPublishObservedToCtx is used to achieve metrics idempotency in case of double applied middleware
    13  func setPublishObservedToCtx(ctx context.Context) context.Context {
    14  	return context.WithValue(ctx, publishObserved, true)
    15  }
    16  
    17  func publishAlreadyObserved(ctx context.Context) bool {
    18  	return ctx.Value(publishObserved) != nil
    19  }
    20  
    21  // setSubscribeObservedToCtx is used to achieve metrics idempotency in case of double applied middleware
    22  func setSubscribeObservedToCtx(ctx context.Context) context.Context {
    23  	return context.WithValue(ctx, subscribeObserved, true)
    24  }
    25  
    26  func subscribeAlreadyObserved(ctx context.Context) bool {
    27  	return ctx.Value(subscribeObserved) != nil
    28  }