context: use `IError` instead of `string` (#9758)
parent
aa49bc2708
commit
524becd523
|
@ -1,19 +1,23 @@
|
||||||
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
||||||
|
// and other request-scoped values across API boundaries and between processes.
|
||||||
|
// Based off: https://github.com/golang/go/tree/master/src/context
|
||||||
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
||||||
module context
|
module context
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
pub const (
|
const (
|
||||||
background = EmptyContext(0)
|
background = EmptyContext(0)
|
||||||
todo = EmptyContext(1)
|
todo = EmptyContext(1)
|
||||||
|
|
||||||
cancel_context_key = 'context.CancelContext'
|
cancel_context_key = 'context.CancelContext'
|
||||||
|
|
||||||
// canceled is the error returned by Context.err when the context is canceled.
|
// canceled is the error returned by Context.err when the context is canceled.
|
||||||
canceled = 'context canceled'
|
canceled = error('context canceled')
|
||||||
|
|
||||||
// deadline_exceeded is the error returned by Context.err when the context's
|
// deadline_exceeded is the error returned by Context.err when the context's
|
||||||
// deadline passes.
|
// deadline passes.
|
||||||
deadline_exceeded = 'context deadline exceeded'
|
deadline_exceeded = error('context deadline exceeded')
|
||||||
)
|
)
|
||||||
|
|
||||||
pub interface Context {
|
pub interface Context {
|
||||||
|
@ -37,7 +41,7 @@ pub interface Context {
|
||||||
// canceled if the context was canceled
|
// canceled if the context was canceled
|
||||||
// or deadline_exceeded if the context's deadline passed.
|
// or deadline_exceeded if the context's deadline passed.
|
||||||
// After err returns a non-nil error, successive calls to err return the same error.
|
// After err returns a non-nil error, successive calls to err return the same error.
|
||||||
err() string
|
err() IError
|
||||||
// Value returns the value associated with this context for key, or nil
|
// Value returns the value associated with this context for key, or nil
|
||||||
// if no value is associated with key. Successive calls to Value with
|
// if no value is associated with key. Successive calls to Value with
|
||||||
// the same key returns the same result.
|
// the same key returns the same result.
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
||||||
|
// and other request-scoped values across API boundaries and between processes.
|
||||||
|
// Based off: https://github.com/golang/go/tree/master/src/context
|
||||||
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
||||||
module context
|
module context
|
||||||
|
|
||||||
import rand
|
import rand
|
||||||
|
@ -6,7 +10,7 @@ import time
|
||||||
|
|
||||||
pub interface Canceler {
|
pub interface Canceler {
|
||||||
id string
|
id string
|
||||||
cancel(remove_from_parent bool, err string)
|
cancel(remove_from_parent bool, err IError)
|
||||||
done() chan int
|
done() chan int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,7 +35,7 @@ mut:
|
||||||
mutex &sync.Mutex
|
mutex &sync.Mutex
|
||||||
done chan int
|
done chan int
|
||||||
children map[string]Canceler
|
children map[string]Canceler
|
||||||
err string
|
err IError
|
||||||
}
|
}
|
||||||
|
|
||||||
// with_cancel returns a copy of parent with a new done channel. The returned
|
// with_cancel returns a copy of parent with a new done channel. The returned
|
||||||
|
@ -52,6 +56,7 @@ fn new_cancel_context(parent Context) &CancelContext {
|
||||||
id: rand.uuid_v4()
|
id: rand.uuid_v4()
|
||||||
context: parent
|
context: parent
|
||||||
mutex: sync.new_mutex()
|
mutex: sync.new_mutex()
|
||||||
|
done: chan int{cap: 2}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,7 +71,7 @@ pub fn (mut ctx CancelContext) done() chan int {
|
||||||
return done
|
return done
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ctx CancelContext) err() string {
|
pub fn (mut ctx CancelContext) err() IError {
|
||||||
ctx.mutex.@lock()
|
ctx.mutex.@lock()
|
||||||
err := ctx.err
|
err := ctx.err
|
||||||
ctx.mutex.unlock()
|
ctx.mutex.unlock()
|
||||||
|
@ -84,13 +89,13 @@ pub fn (ctx CancelContext) str() string {
|
||||||
return context_name(ctx.context) + '.with_cancel'
|
return context_name(ctx.context) + '.with_cancel'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut ctx CancelContext) cancel(remove_from_parent bool, err string) {
|
fn (mut ctx CancelContext) cancel(remove_from_parent bool, err IError) {
|
||||||
if err == '' {
|
if err.str() == 'none' {
|
||||||
panic('context: internal error: missing cancel error')
|
panic('context: internal error: missing cancel error')
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.mutex.@lock()
|
ctx.mutex.@lock()
|
||||||
if ctx.err != '' {
|
if ctx.err.str() != 'none' {
|
||||||
ctx.mutex.unlock()
|
ctx.mutex.unlock()
|
||||||
// already canceled
|
// already canceled
|
||||||
return
|
return
|
||||||
|
@ -141,7 +146,7 @@ fn propagate_cancel(parent Context, mut child Canceler) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.err != '' {
|
if p.err.str() != 'none' {
|
||||||
// parent has already been canceled
|
// parent has already been canceled
|
||||||
child.cancel(false, p.err)
|
child.cancel(false, p.err)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
||||||
|
// and other request-scoped values across API boundaries and between processes.
|
||||||
|
// Based off: https://github.com/golang/go/tree/master/src/context
|
||||||
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
||||||
module context
|
module context
|
||||||
|
|
||||||
import rand
|
import rand
|
||||||
|
@ -43,7 +47,7 @@ pub fn with_deadline(parent Context, d time.Time) Context {
|
||||||
return Context(ctx)
|
return Context(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.cancel_ctx.err() == '' {
|
if ctx.err().str() == 'none' {
|
||||||
go fn (mut ctx TimerContext, dur time.Duration) {
|
go fn (mut ctx TimerContext, dur time.Duration) {
|
||||||
time.sleep(dur)
|
time.sleep(dur)
|
||||||
ctx.cancel(true, deadline_exceeded)
|
ctx.cancel(true, deadline_exceeded)
|
||||||
|
@ -68,7 +72,7 @@ pub fn (mut ctx TimerContext) done() chan int {
|
||||||
return ctx.cancel_ctx.done()
|
return ctx.cancel_ctx.done()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ctx TimerContext) err() string {
|
pub fn (mut ctx TimerContext) err() IError {
|
||||||
return ctx.cancel_ctx.err()
|
return ctx.cancel_ctx.err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,7 +80,7 @@ pub fn (ctx TimerContext) value(key string) ?voidptr {
|
||||||
return ctx.cancel_ctx.value(key)
|
return ctx.cancel_ctx.value(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (mut ctx TimerContext) cancel(remove_from_parent bool, err string) {
|
pub fn (mut ctx TimerContext) cancel(remove_from_parent bool, err IError) {
|
||||||
ctx.cancel_ctx.cancel(false, err)
|
ctx.cancel_ctx.cancel(false, err)
|
||||||
if remove_from_parent {
|
if remove_from_parent {
|
||||||
// Remove this TimerContext from its parent CancelContext's children.
|
// Remove this TimerContext from its parent CancelContext's children.
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
||||||
|
// and other request-scoped values across API boundaries and between processes.
|
||||||
|
// Based off: https://github.com/golang/go/tree/master/src/context
|
||||||
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
||||||
module context
|
module context
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
@ -18,8 +22,9 @@ pub fn (ctx EmptyContext) done() chan int {
|
||||||
return ch
|
return ch
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx EmptyContext) err() string {
|
pub fn (ctx EmptyContext) err() IError {
|
||||||
return ''
|
// TODO: Change this to `none`
|
||||||
|
return none_
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx EmptyContext) value(key string) ?voidptr {
|
pub fn (ctx EmptyContext) value(key string) ?voidptr {
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
module context
|
||||||
|
|
||||||
|
const none_ = IError(&None{})
|
||||||
|
|
||||||
|
struct None {
|
||||||
|
msg string
|
||||||
|
code int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (_ None) str() string {
|
||||||
|
return 'none'
|
||||||
|
}
|
|
@ -1,3 +1,7 @@
|
||||||
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
||||||
|
// and other request-scoped values across API boundaries and between processes.
|
||||||
|
// Based off: https://github.com/golang/go/tree/master/src/context
|
||||||
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
||||||
module context
|
module context
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
@ -37,7 +41,7 @@ pub fn (ctx ValueContext) done() chan int {
|
||||||
return ctx.context.done()
|
return ctx.context.done()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (ctx ValueContext) err() string {
|
pub fn (ctx ValueContext) err() IError {
|
||||||
return ctx.context.err()
|
return ctx.context.err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue