Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

About the Implementation Logic of SendToChannel Function #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wyyolo opened this issue Feb 21, 2024 · 1 comment
Closed

About the Implementation Logic of SendToChannel Function #144

wyyolo opened this issue Feb 21, 2024 · 1 comment

Comments

@wyyolo
Copy link
Contributor

wyyolo commented Feb 21, 2024

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
@RobertIndie
Copy link
Contributor

Just reviewed your code and left some comments: #140 (review)

Option 1 sounds good to me. We don't need to give a strong guarantee for the priority. If the SendToChannel returns true, that doesn't mean the ctx hasn't done.

@wyyolo wyyolo closed this as completed Feb 26, 2024
@FunctionStream FunctionStream locked and limited conversation to collaborators Feb 26, 2024
@RobertIndie RobertIndie converted this issue into discussion #148 Feb 26, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants