diff --git a/api/stream/client.go b/api/stream/client.go index f2ff896..dc8d222 100644 --- a/api/stream/client.go +++ b/api/stream/client.go @@ -1,7 +1,6 @@ package stream import ( - "sync" "time" "github.com/gorilla/websocket" @@ -26,7 +25,7 @@ type client struct { write chan *model.Message userID uint token string - once sync.Once + once once } func newClient(conn *websocket.Conn, userID uint, token string, onClose func(*client)) *client { diff --git a/api/stream/once.go b/api/stream/once.go new file mode 100644 index 0000000..83e0a2d --- /dev/null +++ b/api/stream/once.go @@ -0,0 +1,36 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package stream + +import ( + "sync/atomic" + "sync" +) + +// Modified version of sync.Once (https://github.com/golang/go/blob/master/src/sync/once.go) +// This version unlocks the mutex early and therefore doesn't hold the lock while executing func f(). +type once struct { + m sync.Mutex + done uint32 +} + +func (o *once) Do(f func()) { + if atomic.LoadUint32(&o.done) == 1 { + return + } + if o.mayExecute() { + f() + } +} + +func (o *once) mayExecute() bool { + o.m.Lock() + defer o.m.Unlock() + if o.done == 0 { + atomic.StoreUint32(&o.done, 1) + return true + } + return false +} \ No newline at end of file