-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsenders.go
More file actions
66 lines (55 loc) · 1.52 KB
/
senders.go
File metadata and controls
66 lines (55 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package channel
import (
"errors"
"fmt"
)
type SenderContext interface {
NextSequence() int32
GetCloseNotify() chan struct{}
}
func NewSingleChSender(ctx SenderContext, msgC chan<- Sendable) Sender {
return &singleChSender{ctx: ctx, msgC: msgC}
}
type singleChSender struct {
ctx SenderContext
msgC chan<- Sendable
}
func (self *singleChSender) CloseNotify() <-chan struct{} {
return self.ctx.GetCloseNotify()
}
func (self *singleChSender) TrySend(s Sendable) (bool, error) {
if err := s.Context().Err(); err != nil {
return false, err
}
s.SetSequence(self.ctx.NextSequence())
select {
case <-s.Context().Done():
if err := s.Context().Err(); err != nil {
return false, TimeoutError{error: fmt.Errorf("timeout waiting to put message in send queue (%w)", err)}
}
return false, TimeoutError{error: errors.New("timeout waiting to put message in send queue")}
case <-self.ctx.GetCloseNotify():
return false, ClosedError{}
case self.msgC <- s:
return true, nil
default:
return false, nil
}
}
func (self *singleChSender) Send(s Sendable) error {
if err := s.Context().Err(); err != nil {
return err
}
s.SetSequence(self.ctx.NextSequence())
select {
case <-s.Context().Done():
if err := s.Context().Err(); err != nil {
return TimeoutError{error: fmt.Errorf("timeout waiting to put message in send queue (%w)", err)}
}
return TimeoutError{error: errors.New("timeout waiting to put message in send queue")}
case <-self.ctx.GetCloseNotify():
return ClosedError{}
case self.msgC <- s:
}
return nil
}