From 274a84385a97f9f73decd88e20df4dddd8428c8b Mon Sep 17 00:00:00 2001 From: Sergey Chubaryan Date: Wed, 21 Aug 2024 18:00:53 +0300 Subject: [PATCH] add extended context --- src/request_context/ctx.go | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/request_context/ctx.go diff --git a/src/request_context/ctx.go b/src/request_context/ctx.go new file mode 100644 index 0000000..2436250 --- /dev/null +++ b/src/request_context/ctx.go @@ -0,0 +1,59 @@ +package request_context + +import ( + "context" + "time" +) + +type RequestContext interface { + context.Context + + RequestId() string + UserId() string +} + +type Opts struct { + RequestId string + UserId string +} + +func New(opts Opts) RequestContext { + return reqCtx{ + requestId: opts.RequestId, + userId: opts.UserId, + } +} + +type reqCtx struct { + ctx context.Context + requestId string + userId string +} + +func (r reqCtx) Deadline() (deadline time.Time, ok bool) { + return r.ctx.Deadline() +} + +func (r reqCtx) Done() <-chan struct{} { + return r.ctx.Done() +} + +func (r reqCtx) Err() error { + return r.ctx.Err() +} + +func (r reqCtx) Value(key any) any { + return r.ctx.Value(key) +} + +func (r reqCtx) String() string { + return r.ctx.Err().Error() +} + +func (r reqCtx) RequestId() string { + return r.requestId +} + +func (r reqCtx) UserId() string { + return r.userId +}