Skip to content

Commit

Permalink
Merge pull request #639 from SNORRIS721/mapcrash
Browse files Browse the repository at this point in the history
Changing filestore.offsets from map[int]msgDef to sync.Map
  • Loading branch information
ackleymi authored May 9, 2024
2 parents 1205f67 + 60846b9 commit 807c9fe
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions store/file/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/pkg/errors"
Expand All @@ -42,7 +43,7 @@ type fileStoreFactory struct {
type fileStore struct {
sessionID quickfix.SessionID
cache quickfix.MessageStore
offsets map[int]msgDef
offsets sync.Map
bodyFname string
headerFname string
sessionFname string
Expand Down Expand Up @@ -106,7 +107,7 @@ func newFileStore(sessionID quickfix.SessionID, dirname string, fileSync bool) (
store := &fileStore{
sessionID: sessionID,
cache: memStore,
offsets: make(map[int]msgDef),
offsets: sync.Map{},
bodyFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "body")),
headerFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "header")),
sessionFname: path.Join(dirname, fmt.Sprintf("%s.%s", sessionPrefix, "session")),
Expand Down Expand Up @@ -206,7 +207,7 @@ func (store *fileStore) populateCache() (creationTimePopulated bool, err error)
if cnt, err := fmt.Fscanf(tmpHeaderFile, "%d,%d,%d\n", &seqNum, &offset, &size); err != nil || cnt != 3 {
break
}
store.offsets[seqNum] = msgDef{offset: offset, size: size}
store.offsets.Store(seqNum, msgDef{offset: offset, size: size})
}
}

Expand Down Expand Up @@ -347,7 +348,7 @@ func (store *fileStore) SaveMessage(seqNum int, msg []byte) error {
}
}

store.offsets[seqNum] = msgDef{offset: offset, size: len(msg)}
store.offsets.Store(seqNum, msgDef{offset: offset, size: len(msg)})
return nil
}

Expand All @@ -360,10 +361,14 @@ func (store *fileStore) SaveMessageAndIncrNextSenderMsgSeqNum(seqNum int, msg []
}

func (store *fileStore) getMessage(seqNum int) (msg []byte, found bool, err error) {
msgInfo, found := store.offsets[seqNum]
msgInfoTemp, found := store.offsets.Load(seqNum)
if !found {
return
}
msgInfo, ok := msgInfoTemp.(msgDef)
if !ok {
return nil, true, fmt.Errorf("incorrect msgInfo type while reading file: %s", store.bodyFname)
}

msg = make([]byte, msgInfo.size)
if _, err = store.bodyFile.ReadAt(msg, msgInfo.offset); err != nil {
Expand Down

0 comments on commit 807c9fe

Please sign in to comment.