quickfixgo / quickfix

The Go FIX Protocol Library :rocket:

Home Page:https://www.quickfixgo.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DynamicSessions are incompatible with any MessageStore except the in memory store

mattrasband opened this issue · comments

Because dynamic sessions don't have a setting associated with them most message stores reject them:

File Message Store:

quickfix/filestore.go

Lines 48 to 51 in 94d7217

sessionSettings, ok := f.settings.SessionSettings()[sessionID]
if !ok {
return nil, fmt.Errorf("unknown session: %v", sessionID)
}

SQL Store:

quickfix/sqlstore.go

Lines 54 to 57 in 94d7217

sessionSettings, ok := f.settings.SessionSettings()[sessionID]
if !ok {
return nil, fmt.Errorf("unknown session: %v", sessionID)
}

Mongo Store:

quickfix/mongostore.go

Lines 45 to 48 in 94d7217

sessionSettings, ok := f.settings.SessionSettings()[sessionID]
if !ok {
return nil, fmt.Errorf("unknown session: %v", sessionID)
}

It seems like in the case the session settings aren't found we should default back to the global settings if possible for dynamic session cases.

If this seems sensible I could work on adding it.

As a workaround, is it safe to compose one of these and in the Create implementation dynamically generate the session and save it to the settings?

It certainly feels hacky, but something like:

type dynamicMessageFactory struct {
	store       quickfix.MessageStoreFactory
	settings *quickfix.Settings
	mu       sync.Mutex
}

func newDynamicMessageFactory(settings *quickfix.Settings, composed quickfix.MessageStoreFactory) quickfix.MessageStoreFactory {
	return &dynamicMessageFactory{
		store:       composed,
		settings: settings,
	}
}

func (dfs *dynamicMessageFactory) Create(sessionID quickfix.SessionID) (msgStore quickfix.MessageStore, err error) {
	dfs.mu.Lock()
	if _, ok := dfs.settings.SessionSettings()[sessionID]; !ok {
		session := quickfix.NewSessionSettings()
		session.Set(config.BeginString, sessionID.BeginString)
		session.Set(config.TargetCompID, sessionID.TargetCompID)
		session.Set(config.TargetSubID, sessionID.TargetSubID)
		session.Set(config.TargetLocationID, sessionID.TargetLocationID)
		session.Set(config.SenderCompID, sessionID.SenderCompID)
		session.Set(config.SenderSubID, sessionID.SenderSubID)
		session.Set(config.SenderLocationID, sessionID.SenderLocationID)
		session.Set(config.SessionQualifier, sessionID.Qualifier)

		if sessionID.TargetCompID == "MD" {
			session.Set(config.PersistMessages, "N")
		}

		dfs.settings.AddSession(session)
	}
	dfs.mu.Unlock()

	return dfs.store.Create(sessionID)
}