github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/ctx_limited.go (about)

     1  package kit
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  )
     7  
     8  // LimitedContext is a wrapper around Context, which limit the capabilities of the original Context.
     9  // This is useful in cases where we need to pass the Context, but we do not want to give access to all
    10  // the exposed methods. For example, this is used in EdgeSelectorFunc.
    11  type LimitedContext struct {
    12  	ctx *Context
    13  }
    14  
    15  func newLimitedContext(ctx *Context) *LimitedContext {
    16  	return &LimitedContext{
    17  		ctx: ctx,
    18  	}
    19  }
    20  
    21  // Context returns a context.WithCancel, which can be used a reference context for
    22  // other context-aware function calls.
    23  // This context will be canceled at the end of Context lifetime.
    24  func (ctx *LimitedContext) Context() context.Context {
    25  	return ctx.ctx.Context()
    26  }
    27  
    28  // In returns the incoming Envelope which contains the client's request.
    29  func (ctx *LimitedContext) In() *Envelope {
    30  	return ctx.ctx.In()
    31  }
    32  
    33  // Conn returns the underlying connection
    34  func (ctx *LimitedContext) Conn() Conn {
    35  	return ctx.ctx.conn
    36  }
    37  
    38  func (ctx *LimitedContext) IsREST() bool {
    39  	return ctx.ctx.IsREST()
    40  }
    41  
    42  // SetHdr sets the common header key-value pairs, so in Out method we do not need to
    43  // repeatedly set those. If you only want to set the header for an envelope, you can
    44  // use Envelope.SetHdr method instead.
    45  func (ctx *LimitedContext) SetHdr(k, v string) {
    46  	ctx.ctx.hdr[k] = v
    47  }
    48  
    49  // SetHdrMap sets the common header key-value pairs, so in Out method we do not need to
    50  // repeatedly set those.
    51  func (ctx *LimitedContext) SetHdrMap(hdr map[string]string) {
    52  	for k, v := range hdr {
    53  		ctx.ctx.hdr[k] = v
    54  	}
    55  }
    56  
    57  func (ctx *LimitedContext) Route() string {
    58  	return ctx.ctx.Route()
    59  }
    60  
    61  func (ctx *LimitedContext) ServiceName() string {
    62  	return ctx.ctx.ServiceName()
    63  }
    64  
    65  func (ctx *LimitedContext) ClusterID() string {
    66  	return ctx.ctx.ClusterID()
    67  }
    68  
    69  func (ctx *LimitedContext) ClusterMembers() ([]string, error) {
    70  	return ctx.ctx.ClusterMembers()
    71  }
    72  
    73  func (ctx *LimitedContext) ClusterStore() ClusterStore {
    74  	return ctx.ctx.ClusterStore()
    75  }
    76  
    77  func (ctx *LimitedContext) SetUserContext(userCtx context.Context) {
    78  	ctx.ctx.SetUserContext(userCtx)
    79  }
    80  
    81  func (ctx *LimitedContext) SetRemoteExecutionTimeout(timeout time.Duration) {
    82  	ctx.ctx.rxt = timeout
    83  }