[英]Hasql's session and IO
I wrote a function 我写了一个函数
app :: Request -> H.Session H.Postgres IO Response
which accepts web requests and builds responses (consulting the database as needed). 它接受Web请求并构建响应(根据需要咨询数据库)。 To actually send out the responses I made a wrapper
为了实际发出回复,我做了一个包装器
runApp :: H.Postgres -> H.SessionSettings -> Application
runApp pg sess req respond =
respond =<< H.session pg sess (app req)
I pass this function to Warp 's runSettings
to loop forever and handle requests: 我将此函数传递给Warp的
runSettings
以永久循环并处理请求:
runSettings appSettings $ runApp pgSettings sessSettings
However this is really bad because it creates a new session for every request which defeats the purpose of the connection pool and prepared statements. 但是这非常糟糕,因为它为每个请求创建了一个新会话,这会破坏连接池和预处理语句的目的。
I would like to call runSettings
inside H.session
rather than the other way around. 我想在
H.session
调用runSettings
而不是相反。 However runSettings
has a signature Settings -> Application -> IO ()
and once inside IO
I have lost access to the session. 但是,
runSettings
有一个签名Settings -> Application -> IO ()
,一旦进入IO
我就失去了对会话的访问权限。 Is there a way to get back inside Session bmr
? 有没有办法回到
Session bmr
?
This is a repost of a question from a private email. 这是私人电子邮件中的问题重新发布。
Yes, in your example you create a new session for every request, which is unacceptable. 是的,在您的示例中,您为每个请求创建一个新会话,这是不可接受的。
First of all, Session
is just and alias to the reader monad transformer , which gives you a direct access to the pool. 首先,
Session
只是读取器monad转换器的别名 ,它可以直接访问池。 So you can always do: 所以你总能这样做:
session postgresSettings sessionSettings $ do
-- session' :: H.Session b m r -> m r
session' <- flip runReaderT <$> ask
let runApp request respond =
respond =<< session' (app request)
liftIO $ do
-- run warp here
Secondly, ReaderT
has a MonadBaseControl
instance, which is intended for similar patterns. 其次,
ReaderT
有一个MonadBaseControl
实例,用于类似的模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.