Make *Context compliant with context.Context
This commit is contained in:
parent
f0f9422fef
commit
bbc9774a96
@ -19,6 +19,9 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go.arsenm.dev/lrpc/codec"
|
"go.arsenm.dev/lrpc/codec"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
@ -33,6 +36,7 @@ type Context struct {
|
|||||||
codec codec.Codec
|
codec codec.Codec
|
||||||
|
|
||||||
doneCh chan struct{}
|
doneCh chan struct{}
|
||||||
|
canceled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeChannel changes the function it's called in into a
|
// MakeChannel changes the function it's called in into a
|
||||||
@ -55,6 +59,26 @@ func (ctx *Context) GetCodec() codec.Codec {
|
|||||||
return ctx.codec
|
return ctx.codec
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deadline always returns the current time and false
|
||||||
|
// as this context does not support deadlines
|
||||||
|
func (ctx *Context) Deadline() (time.Time, bool) {
|
||||||
|
return time.Now(), false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value always returns nil as this context stores no values
|
||||||
|
func (ctx *Context) Value(_ any) any {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Err returns context.Canceled if the context was canceled,
|
||||||
|
// otherwise nil
|
||||||
|
func (ctx *Context) Err() error {
|
||||||
|
if ctx.canceled {
|
||||||
|
return context.Canceled
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Done returns a channel that will be closed when
|
// Done returns a channel that will be closed when
|
||||||
// the context is canceled, such as when ChannelDone
|
// the context is canceled, such as when ChannelDone
|
||||||
// is called by the client
|
// is called by the client
|
||||||
@ -64,5 +88,6 @@ func (ctx *Context) Done() <-chan struct{} {
|
|||||||
|
|
||||||
// Cancel cancels the context
|
// Cancel cancels the context
|
||||||
func (ctx *Context) Cancel() {
|
func (ctx *Context) Cancel() {
|
||||||
|
ctx.canceled = true
|
||||||
close(ctx.doneCh)
|
close(ctx.doneCh)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user