Skip to content

proposal: context: Sleep(ctx, dur) #73295

@slsyy

Description

@slsyy

Proposal Details

I propose such a function to the context package:

func Sleep(ctx Context, dur time.Duration) error {
    select {
      case <- ctx.Done():
          return ctx.Err()
      case <- time.After(d):
          return nil
    } 
}

Rationale time.Sleep is a dangerous to use in a context-aware code as it is blocking. The only option is to use an inlined version of an above select{} statement, but it is quite verbose, so people use time.Sleep due to laziness

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions