This commit optimize AtomicError using atomic.Value. Benchmarks: name old time/op new time/op delta AtomicError/Load-6 305ns ±11% 12ns ± 6% -96.18% (p=0.000 n=10+10) AtomicError/Set-6 314ns ±16% 14ns ± 2% -95.61% (p=0.000 n=10+9)
19 lines
265 B
Go
19 lines
265 B
Go
package errorx
|
|
|
|
import "sync/atomic"
|
|
|
|
type AtomicError struct {
|
|
err atomic.Value // error
|
|
}
|
|
|
|
func (ae *AtomicError) Set(err error) {
|
|
ae.err.Store(err)
|
|
}
|
|
|
|
func (ae *AtomicError) Load() error {
|
|
if v := ae.err.Load(); v != nil {
|
|
return v.(error)
|
|
}
|
|
return nil
|
|
}
|