Skip to content

Commit

Permalink
do not hang request on entity not found (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniRamirezM authored Oct 30, 2023
1 parent 6fc6638 commit 1545001
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
15 changes: 15 additions & 0 deletions datastreamer/datastreamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ var (
testBookmark = TestBookmark{
FieldA: []byte{0, 1, 0, 0, 0, 0, 0, 0, 0},
}

nonAddedBookmark = TestBookmark{
FieldA: []byte{0, 2, 0, 0, 0, 0, 0, 0, 0},
}
)

func deleteFiles() error {
Expand Down Expand Up @@ -146,11 +150,22 @@ func TestClient(t *testing.T) {
err = client.Start()
require.NoError(t, err)

// Should succeed
client.FromBookmark = testBookmark.Encode()
err = client.ExecCommand(datastreamer.CmdBookmark)
require.NoError(t, err)
require.Equal(t, testEntry.Encode(), client.Entry.Data)

// Should fail because the bookmark is not added
client.FromBookmark = nonAddedBookmark.Encode()
err = client.ExecCommand(datastreamer.CmdBookmark)
require.EqualError(t, datastreamer.ErrEntryNotFound, err.Error())

// Should fail because the entry is not added
client.FromEntry = 3
err = client.ExecCommand(datastreamer.CmdEntry)
require.EqualError(t, datastreamer.ErrEntryNotFound, err.Error())

client.FromEntry = 2
err = client.ExecCommand(datastreamer.CmdEntry)
require.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions datastreamer/streamclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,15 @@ func (c *StreamClient) ExecCommand(cmd Command) error {
c.Header = h
case CmdEntry:
e := c.getEntry()
if e.Type == EntryTypeNotFound {
return ErrEntryNotFound
}
c.Entry = e
case CmdBookmark:
e := c.getEntry()
if e.Type == EntryTypeNotFound {
return ErrEntryNotFound
}
c.Entry = e
}

Expand Down
14 changes: 12 additions & 2 deletions datastreamer/streamserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datastreamer
import (
"encoding/binary"
"io"
"math"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -31,6 +32,9 @@ type StreamType uint64
// CommandError type for the command responses
type CommandError uint32

// EntryTypeNotFound is used to indicate that the entry type is not found
const EntryTypeNotFound = math.MaxUint32

const (
maxConnections = 100 // Maximum number of connected clients
streamBuffer = 256 // Buffers for the stream channel
Expand Down Expand Up @@ -823,7 +827,10 @@ func (s *StreamServer) processCmdEntry(clientId string) error {
// Get the requested entry
entry, err := s.GetEntry(entryNumber)
if err != nil {
return err
log.Errorf("Error getting entry %d: %v", entryNumber, err)
entry = FileEntry{}
entry.Length = FixedSizeFileEntry
entry.Type = EntryTypeNotFound
}
entry.packetType = PtDataRsp
binaryEntry := encodeFileEntryToBinary(entry)
Expand Down Expand Up @@ -870,7 +877,10 @@ func (s *StreamServer) processCmdBookmark(clientId string) error {
// Get the requested bookmark
entry, err := s.GetFirstEventAfterBookmark(bookmark)
if err != nil {
return err
log.Errorf("Error getting bookmark %v: %v", bookmark, err)
entry = FileEntry{}
entry.Length = FixedSizeFileEntry
entry.Type = EntryTypeNotFound
}
entry.packetType = PtDataRsp
binaryEntry := encodeFileEntryToBinary(entry)
Expand Down

0 comments on commit 1545001

Please sign in to comment.