Skip to content

Commit

Permalink
Pass peristent store errors up to the router level
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefmansy1 committed Mar 11, 2023
1 parent eaa8907 commit 2845740
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
22 changes: 17 additions & 5 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const apiProtocol = "api"

// New creates a new Gateway object associated with the specified router and
// following the given configuration.
func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway {
func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) (*Gateway, error) {
logger := rootLogger.WithFields(logrus.Fields{"prefix": "gateway"})

cache, _ := lru.New(5000)
Expand All @@ -67,13 +67,25 @@ func New(rootLogger *logrus.Logger, cfg *config.Gateway, r *Router) *Gateway {
persistentMessageStorePath, usePersistent := gw.Config.GetString("PersistentMessageStorePath")
if usePersistent {
rootPath := fmt.Sprintf("%s/%s", persistentMessageStorePath, gw.Name)
os.MkdirAll(rootPath, os.ModePerm)
err := os.MkdirAll(rootPath, os.ModePerm)
if err != nil {
return nil, err
}

gw.MessageStore = gw.getMessageMapStore(fmt.Sprintf("%s/Messages", rootPath))
gw.CanonicalStore = gw.getMessageMapStore(fmt.Sprintf("%s/Canonical", rootPath))
MessageStore, err := gw.getMessageMapStore(fmt.Sprintf("%s/Messages", rootPath))
if err != nil {
return nil, err
}
gw.MessageStore = MessageStore

CanonicalStore, err := gw.getMessageMapStore(fmt.Sprintf("%s/Canonical", rootPath))
if err != nil {
return nil, err
}
gw.CanonicalStore = CanonicalStore
}

return gw
return gw, nil
}

func (gw *Gateway) SetMessageMap(canonicalMsgID string, msgIDs []*BrMsgID) {
Expand Down
7 changes: 4 additions & 3 deletions gateway/persistent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import (
"github.com/philippgille/gokv/encoding"
)

func (gw *Gateway) getMessageMapStore(path string) gokv.Store {
func (gw *Gateway) getMessageMapStore(path string) (gokv.Store, error) {
options := badgerdb.Options{
Dir: path,
Codec: encoding.Gob,
}

store, err := badgerdb.NewStore(options)
if err != nil {
gw.logger.Error(err)
gw.logger.Errorf("Could not connect to db: %s", path)
gw.logger.Error(err)
return nil, err
}

return store
return store, nil
}

func (gw *Gateway) getCanonicalMessageFromStore(messageID string) string {
Expand Down
7 changes: 6 additions & 1 deletion gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ func NewRouter(rootLogger *logrus.Logger, cfg config.Config, bridgeMap map[strin
if _, ok := r.Gateways[entry.Name]; ok {
return nil, fmt.Errorf("Gateway with name %s already exists", entry.Name)
}
r.Gateways[entry.Name] = New(rootLogger, entry, r)
gw, err := New(rootLogger, entry, r)
if err != nil {
return nil, err
}

r.Gateways[entry.Name] = gw
}
return r, nil
}
Expand Down

0 comments on commit 2845740

Please sign in to comment.