@@ -71,12 +71,30 @@ func CreateHandleMessage() func(*fiber.Ctx) error {
7171 accountIdStr := strconv .Itoa (int (accountId ))
7272 conn := ConnectionsCache [accountIdStr ][username ].Connection
7373 if conn == nil {
74- log .Warnf ("CreateHandleMessage - produce: Connection does not exist" )
75- c .Status (fiber .StatusInternalServerError )
76- return c .JSON (& fiber.Map {
77- "success" : false ,
78- "error" : "Server error" ,
79- })
74+ conn , err = Connect (userData .Password , username , userData .ConnectionToken , int (accountId ))
75+ if err != nil {
76+ errMsg := strings .ToLower (err .Error ())
77+ if strings .Contains (errMsg , ErrorMsgAuthorizationViolation ) || strings .Contains (errMsg , "token" ) || strings .Contains (errMsg , ErrorMsgMissionAccountId ) {
78+ log .Warnf ("Could not establish new connection with the broker: Authentication error" )
79+ return c .Status (401 ).JSON (fiber.Map {
80+ "message" : "Unauthorized" ,
81+ })
82+ }
83+
84+ log .Errorf ("Could not establish new connection with the broker: %s" , err .Error ())
85+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {
86+ "message" : "Server error" ,
87+ })
88+ }
89+ if ConnectionsCache [accountIdStr ] == nil {
90+ ConnectionsCacheLock .Lock ()
91+ ConnectionsCache [accountIdStr ] = make (map [string ]Connection )
92+ ConnectionsCacheLock .Unlock ()
93+ }
94+
95+ ConnectionsCacheLock .Lock ()
96+ ConnectionsCache [accountIdStr ][username ] = Connection {Connection : conn , ExpirationTime : userData .TokenExpiry }
97+ ConnectionsCacheLock .Unlock ()
8098 }
8199 err = conn .Produce (stationName , "rest-gateway" , message , []memphis.ProducerOpt {}, []memphis.ProduceOpt {memphis .MsgHeaders (hdrs )})
82100 if err != nil {
@@ -144,12 +162,30 @@ func CreateHandleBatch() func(*fiber.Ctx) error {
144162 accountIdStr := strconv .Itoa (int (accountId ))
145163 conn := ConnectionsCache [accountIdStr ][username ].Connection
146164 if conn == nil {
147- log .Warnf ("CreateHandleBatch - produce: Connection does not exist" )
148- c .Status (fiber .StatusInternalServerError )
149- return c .JSON (& fiber.Map {
150- "success" : false ,
151- "error" : "Server error" ,
152- })
165+ conn , err = Connect (userData .Password , username , userData .ConnectionToken , int (accountId ))
166+ if err != nil {
167+ errMsg := strings .ToLower (err .Error ())
168+ if strings .Contains (errMsg , ErrorMsgAuthorizationViolation ) || strings .Contains (errMsg , "token" ) || strings .Contains (errMsg , ErrorMsgMissionAccountId ) {
169+ log .Warnf ("Could not establish new connection with the broker: Authentication error" )
170+ return c .Status (401 ).JSON (fiber.Map {
171+ "message" : "Unauthorized" ,
172+ })
173+ }
174+
175+ log .Errorf ("Could not establish new connection with the broker: %s" , err .Error ())
176+ return c .Status (fiber .StatusInternalServerError ).JSON (fiber.Map {
177+ "message" : "Server error" ,
178+ })
179+ }
180+ if ConnectionsCache [accountIdStr ] == nil {
181+ ConnectionsCacheLock .Lock ()
182+ ConnectionsCache [accountIdStr ] = make (map [string ]Connection )
183+ ConnectionsCacheLock .Unlock ()
184+ }
185+
186+ ConnectionsCacheLock .Lock ()
187+ ConnectionsCache [accountIdStr ][username ] = Connection {Connection : conn , ExpirationTime : userData .TokenExpiry }
188+ ConnectionsCacheLock .Unlock ()
153189 }
154190
155191 errCount := 0
0 commit comments