Prevent multiple close of channel
Pyxis/orion/pipeline/head There was a failure building this commit Details

This commit is contained in:
wpetit 2021-01-26 18:01:06 +01:00
parent e7bec24f0f
commit c659a98587
1 changed files with 9 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package reachview
import ( import (
"context" "context"
"sync"
gosocketio "forge.cadoles.com/Pyxis/golang-socketio" gosocketio "forge.cadoles.com/Pyxis/golang-socketio"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -31,6 +32,8 @@ type Broadcast struct {
func (c *Client) Broadcast(ctx context.Context) (chan Broadcast, error) { func (c *Client) Broadcast(ctx context.Context) (chan Broadcast, error) {
out := make(chan Broadcast) out := make(chan Broadcast)
closer := new(sync.Once)
handler := func(_ *gosocketio.Channel, data interface{}) { handler := func(_ *gosocketio.Channel, data interface{}) {
res := Broadcast{} res := Broadcast{}
if err := mapstructure.WeakDecode(data, &res); err != nil { if err := mapstructure.WeakDecode(data, &res); err != nil {
@ -38,8 +41,12 @@ func (c *Client) Broadcast(ctx context.Context) (chan Broadcast, error) {
} }
select { select {
case <-ctx.Done(): case <-ctx.Done():
c.Off(eventBroadcast) closer.Do(func() {
close(out) c.Off(eventBroadcast)
close(out)
})
return
default: default:
out <- res out <- res
} }