Skip to content

About the Implementation Logic of SendToChannel Function #144

@wyyolo

Description

@wyyolo

func SendToChannel[T any](ctx context.Context, c chan<- T, e interface{}) bool {
select {
case c <- e.(T): // It will panic if e is not of type T or a type that can be converted to T.
return true
case <-ctx.Done():
close(c)
return false
}
}

I think we need to implement "case" priority control. If chan has a buffer, when "c<- e. (T)" and "<- ctx. Done()" both satisfy simultaneously, "select" will randomly execute one of them. What should our goal be?

  1. After ctx is cancelled, "c <- e.(T)" can still be executed, and then "<-ctx.Done()" can be executed to close chan.
  2. When ctx is cancelled, priority is given to execution, that is, "<-ctx.Done()" has a higher priority

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions