Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with latest unstable Node version (v14.1.0) #50

Open
battermann opened this issue May 6, 2020 · 2 comments
Open

Problem with latest unstable Node version (v14.1.0) #50

battermann opened this issue May 6, 2020 · 2 comments

Comments

@battermann
Copy link

After upgrading Node to the latest version my application stopped working properly even though there were no code changes.
I could trace it down to the function withConnection that did not return at all, no errors, app seemed to hold there.
After downgrading Node to the latest stable version (v12.16.3) it worked again as expected.

Here is the code:

module EventStore where

import Prelude
import Control.Monad.Except.Trans (class MonadError, ExceptT, runExceptT, withExceptT)
import Data.Argonaut (class EncodeJson, Json, jsonEmptyObject)
import Data.Argonaut.Encode.Combinators ((:=), (~>))
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
import Database.PostgreSQL.PG (class FromSQLRow, Connection, PGError, Pool, Query(Query), defaultPoolConfiguration, fromSQLValue, newPool, query)
import Database.PostgreSQL.PG as PG
import Effect.Aff (Aff)
import Effect.Aff.Class (class MonadAff)
import Effect.Class (liftEffect)
import Types (EventId(..), Result, Limit(..))

type EventStore
  = { events :: EventId -> Limit -> Result (Array Event) }

newtype Event
  = Event
  { id :: EventId
  , data :: Json
  }

instance eventToJson :: EncodeJson Event where
  encodeJson (Event event) =
    "offset" := unwrap event.id
      ~> ("payload" := event.data)
      ~> jsonEmptyObject
    where
    unwrap (EventId id) = id

instance fromSQLRowFoo :: FromSQLRow Event where
  fromSQLRow [ idValue, dataValue ] = (\id data' -> Event { id: EventId id, data: data' }) <$> fromSQLValue idValue <*> fromSQLValue dataValue
  fromSQLRow _ = Left "Expecting exactly two more fields."

type PG a
  = ExceptT PGError Aff a

withConnection ::  a. Pool -> (Connection -> PG a) -> PG a
withConnection = PG.withConnection runExceptT

newtype DbName
  = DbName String

newtype DbUser
  = DbUser String

newtype DbPass
  = DbPass String

newtype DbHost
  = DbHost String

eventStore :: DbHost -> DbName -> DbUser -> DbPass -> Result EventStore
eventStore (DbHost host) (DbName name) (DbUser user) (DbPass pass) = do
  pool <- liftEffect $ newPool config
  pure { events: \id limit -> withExceptT show $ withConnection pool (events id limit) }
  where
  config =
    (defaultPoolConfiguration name)
      { idleTimeoutMillis = Just 1000
      , user = Just user
      , password = Just pass
      , host = Just host
      , max = Just 2
      }

events ::  m. Bind m => MonadError PGError m => MonadAff m => EventId -> Limit -> Connection -> m (Array Event)
events (EventId id) (Limit limit) conn = query conn (Query sql) (id /\ limit)
  where
  sql =
    """
      SELECT id, data
      FROM events
      WHERE id > $1
      ORDER BY id ASC
      LIMIT $2
    """
@paluh
Copy link
Collaborator

paluh commented May 6, 2020

@battermann Thanks a lot for the detailed report!

@paluh
Copy link
Collaborator

paluh commented Nov 11, 2020

@battermann Would you be so kind and verify if the issue is caused by outdated pg version:

brianc/node-postgres#2069

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants