Skip to content

Commit 6a8ac6c

Browse files
committed
remove some bottlenecks
1 parent 69bd65e commit 6a8ac6c

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

client.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ type Client struct {
1414
flush http.Flusher
1515
write io.Writer
1616
ctx context.Context
17-
events chan *Event
17+
events chan Event
1818
closed bool
1919
waiter sync.WaitGroup
2020
lock sync.Mutex
21-
lastFlush time.Time
22-
lastWrite time.Time
21+
lastFlush uint64
22+
lastWrite uint64
2323
}
2424

2525
// NewClient creates a client wrapping a response writer.
@@ -30,7 +30,7 @@ type Client struct {
3030
// Returns nil on error.
3131
func NewClient(w http.ResponseWriter, req *http.Request) *Client {
3232
c := &Client{
33-
events: make(chan *Event, 100),
33+
events: make(chan Event, 100),
3434
write: w,
3535
}
3636

@@ -62,24 +62,24 @@ func NewClient(w http.ResponseWriter, req *http.Request) *Client {
6262
// This does not block until the event has been sent,
6363
// however it could block if the event queue is full.
6464
// Returns an error if the Client has disconnected
65-
func (c *Client) Send(ev *Event) error {
65+
func (c *Client) Send(ev Event) error {
6666
if c.closed {
6767
return io.ErrClosedPipe
6868
}
69-
c.events <- ev.Clone()
69+
c.events <- ev
7070
return nil
7171
}
7272

7373
// Send queues an event to be sent to the client.
7474
// This guarantees not block until the event has been sent.
7575
// Returns true if blocked
7676
// Returns an error if the Client has disconnected
77-
func (c *Client) SendNonBlocking(ev *Event) (bool, error) {
77+
func (c *Client) SendNonBlocking(ev Event) (bool, error) {
7878
if c.closed {
7979
return false, io.ErrClosedPipe
8080
}
8181
select {
82-
case c.events <- ev.Clone():
82+
case c.events <- ev:
8383
default:
8484
return true, nil
8585
}
@@ -114,8 +114,8 @@ func (c *Client) run() {
114114

115115
// send the event
116116
c.lock.Lock()
117-
io.Copy(c.write, ev)
118-
c.lastWrite = time.Now()
117+
io.Copy(c.write, &ev)
118+
c.lastWrite += 1
119119
c.lock.Unlock()
120120

121121
case <-done:
@@ -137,7 +137,7 @@ func (c *Client) flusher() {
137137
if c.closed {
138138
break
139139
}
140-
if c.lastFlush.Before(c.lastWrite) {
140+
if c.lastFlush < c.lastWrite {
141141
c.lastFlush = c.lastWrite
142142
c.flush.Flush()
143143
}

stream.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (s *Stream) Broadcast(e *Event) {
8585
defer s.listLock.RUnlock()
8686

8787
for cli := range s.clients {
88-
cli.Send(e)
88+
cli.Send(*e.Clone())
8989
}
9090
}
9191

@@ -127,7 +127,7 @@ func (s *Stream) Publish(topic string, e *Event) {
127127

128128
for cli, topics := range s.clients {
129129
if topics[topic] {
130-
cli.Send(e)
130+
cli.Send(*e.Clone())
131131
}
132132
}
133133
}

0 commit comments

Comments
 (0)