Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] Possible delays in delivering messages #895

Merged
merged 1 commit into from
Feb 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion server/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ type outbound struct {
pb int64 // Total pending/queued bytes.
pm int64 // Total pending/queued messages.
sg *sync.Cond // Flusher conditional for signaling.
sgw bool // Indicate flusher is waiting on condition wait.
wdl time.Duration // Snapshot fo write deadline.
mp int64 // snapshot of max pending.
fsp int // Flush signals that are pending from readLoop's pcd.
Expand Down Expand Up @@ -631,7 +632,9 @@ func (c *client) writeLoop() {
c.mu.Lock()
if waitOk && (c.out.pb == 0 || c.out.fsp > 0) && len(c.out.nb) == 0 && !c.flags.isSet(clearConnection) {
// Wait on pending data.
c.out.sgw = true
c.out.sg.Wait()
c.out.sgw = false
}
// Flush data
waitOk = c.flushOutbound()
Expand Down Expand Up @@ -917,13 +920,21 @@ func (c *client) flushOutbound() bool {
}
}

// Check that if there is still data to send and writeLoop is in wait,
// we need to signal
if c.out.pb > 0 && c.out.sgw {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just signal, why do we need the bool? I think we may just want to signal on partial?

c.out.sg.Signal()
}

return true
}

// flushSignal will use server to queue the flush IO operation to a pool of flushers.
// Lock must be held.
func (c *client) flushSignal() {
c.out.sg.Signal()
if c.out.sgw {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we think this is saving us much?

Copy link
Member Author

@kozlovic kozlovic Feb 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick and dirty benchmark of simulating signaling a routine going into a wait, with and without a check with boolean:

Benchmark_SignalWithoutCheck-8   	50000000	        41.6 ns/op
Benchmark____SignalWithCheck-8   	100000000	        19.9 ns/op

But more importantly, even if we don't gain much, why not checking? Signaling while not in wait is just wasted because a signal while not in wait is lost anyway. Since we use mutex to protect wait()/signal(), it makes sense to me to know if we are in wait or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that is convincing!

c.out.sg.Signal()
}
}

func (c *client) traceMsg(msg []byte) {
Expand Down
63 changes: 63 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,66 @@ func TestGetRandomIP(t *testing.T) {
}
}
}

type slowWriteConn struct {
net.Conn
}

func (swc *slowWriteConn) Write(b []byte) (int, error) {
// Limit the write to 10 bytes at a time.
max := len(b)
if max > 10 {
max = 10
}
return swc.Conn.Write(b[:max])
}

func TestClientWriteLoopStall(t *testing.T) {
opts := DefaultOptions()
s := RunServer(opts)
defer s.Shutdown()

errCh := make(chan error, 1)

url := fmt.Sprintf("nats://%s:%d", opts.Host, opts.Port)
nc, err := nats.Connect(url,
nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, e error) {
select {
case errCh <- e:
default:
}
}))
if err != nil {
t.Fatalf("Error on connect: %v", err)
}
defer nc.Close()
sub, err := nc.SubscribeSync("foo")
if err != nil {
t.Fatalf("Error on subscribe: %v", err)
}
cid, _ := nc.GetClientID()

sender, err := nats.Connect(url)
if err != nil {
t.Fatalf("Error on connect: %v", err)
}
defer sender.Close()

c := s.getClient(cid)
c.mu.Lock()
c.nc = &slowWriteConn{Conn: c.nc}
c.mu.Unlock()

sender.Publish("foo", make([]byte, 100))

if _, err := sub.NextMsg(3 * time.Second); err != nil {
t.Fatalf("WriteLoop has stalled!")
}

// Make sure that we did not get any async error
select {
case e := <-errCh:
t.Fatalf("Got error: %v", e)
case <-time.After(250 * time.Millisecond):
}
}