File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed
Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ package service
2+
3+ import "sync"
4+
5+ // SynchronizedSender is a sender function with an extra protection for
6+ // concurrent writes, if multiple threads call the Send method they will
7+ // be blocked and serialized.
8+ type SynchronizedSender [T any ] struct {
9+ lock sync.Mutex
10+ protectedSend func (T ) error
11+ }
12+
13+ // Send the message using the underlyng stream.
14+ func (s * SynchronizedSender [T ]) Send (value T ) error {
15+ s .lock .Lock ()
16+ err := s .protectedSend (value )
17+ s .lock .Unlock ()
18+ return err
19+ }
20+
21+ // NewSynchronizedSend takes a Send function and wraps it in a SynchronizedSender
22+ func NewSynchronizedSend [T any ](send func (T ) error ) * SynchronizedSender [T ] {
23+ return & SynchronizedSender [T ]{
24+ protectedSend : send ,
25+ }
26+ }
You can’t perform that action at this time.
0 commit comments