Rename ConnPool to SessionManager
This commit is contained in:
parent
800a83059d
commit
efd401bfbf
3 changed files with 20 additions and 20 deletions
|
@ -91,7 +91,7 @@ func handleLogin(ectx echo.Context) error {
|
||||||
return ctx.Render(http.StatusOK, "login.html", nil)
|
return ctx.Render(http.StatusOK, "login.html", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := ctx.server.imap.pool.Put(conn, username, password)
|
token, err := ctx.server.sessions.Put(conn, username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to put connection in pool: %v", err)
|
return fmt.Errorf("failed to put connection in pool: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,12 +15,12 @@ const cookieName = "koushin_session"
|
||||||
const messagesPerPage = 50
|
const messagesPerPage = 50
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
|
sessions *SessionManager
|
||||||
|
|
||||||
imap struct {
|
imap struct {
|
||||||
host string
|
host string
|
||||||
tls bool
|
tls bool
|
||||||
insecure bool
|
insecure bool
|
||||||
|
|
||||||
pool *ConnPool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
smtp struct {
|
smtp struct {
|
||||||
|
@ -76,11 +76,11 @@ func (s *Server) parseSMTPURL(smtpURL string) error {
|
||||||
|
|
||||||
func newServer(imapURL, smtpURL string) (*Server, error) {
|
func newServer(imapURL, smtpURL string) (*Server, error) {
|
||||||
s := &Server{}
|
s := &Server{}
|
||||||
|
s.sessions = NewSessionManager()
|
||||||
|
|
||||||
if err := s.parseIMAPURL(imapURL); err != nil {
|
if err := s.parseIMAPURL(imapURL); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.imap.pool = NewConnPool()
|
|
||||||
|
|
||||||
if smtpURL != "" {
|
if smtpURL != "" {
|
||||||
if err := s.parseSMTPURL(smtpURL); err != nil {
|
if err := s.parseSMTPURL(smtpURL); err != nil {
|
||||||
|
@ -166,7 +166,7 @@ func New(e *echo.Echo, options *Options) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.session, err = ctx.server.imap.pool.Get(cookie.Value)
|
ctx.session, err = ctx.server.sessions.Get(cookie.Value)
|
||||||
if err == ErrSessionExpired {
|
if err == ErrSessionExpired {
|
||||||
ctx.setToken("")
|
ctx.setToken("")
|
||||||
return ctx.Redirect(http.StatusFound, "/login")
|
return ctx.Redirect(http.StatusFound, "/login")
|
||||||
|
|
|
@ -34,31 +34,31 @@ func (s *Session) Do(f func(*imapclient.Client) error) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: expiration timer
|
// TODO: expiration timer
|
||||||
type ConnPool struct {
|
type SessionManager struct {
|
||||||
locker sync.Mutex
|
locker sync.Mutex
|
||||||
sessions map[string]*Session
|
sessions map[string]*Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnPool() *ConnPool {
|
func NewSessionManager() *SessionManager {
|
||||||
return &ConnPool{
|
return &SessionManager{
|
||||||
sessions: make(map[string]*Session),
|
sessions: make(map[string]*Session),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pool *ConnPool) Get(token string) (*Session, error) {
|
func (sm *SessionManager) Get(token string) (*Session, error) {
|
||||||
pool.locker.Lock()
|
sm.locker.Lock()
|
||||||
defer pool.locker.Unlock()
|
defer sm.locker.Unlock()
|
||||||
|
|
||||||
session, ok := pool.sessions[token]
|
session, ok := sm.sessions[token]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrSessionExpired
|
return nil, ErrSessionExpired
|
||||||
}
|
}
|
||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pool *ConnPool) Put(imapConn *imapclient.Client, username, password string) (token string, err error) {
|
func (sm *SessionManager) Put(imapConn *imapclient.Client, username, password string) (token string, err error) {
|
||||||
pool.locker.Lock()
|
sm.locker.Lock()
|
||||||
defer pool.locker.Unlock()
|
defer sm.locker.Unlock()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var err error
|
var err error
|
||||||
|
@ -68,12 +68,12 @@ func (pool *ConnPool) Put(imapConn *imapclient.Client, username, password string
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := pool.sessions[token]; !ok {
|
if _, ok := sm.sessions[token]; !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.sessions[token] = &Session{
|
sm.sessions[token] = &Session{
|
||||||
imapConn: imapConn,
|
imapConn: imapConn,
|
||||||
username: username,
|
username: username,
|
||||||
password: password,
|
password: password,
|
||||||
|
@ -82,9 +82,9 @@ func (pool *ConnPool) Put(imapConn *imapclient.Client, username, password string
|
||||||
go func() {
|
go func() {
|
||||||
<-imapConn.LoggedOut()
|
<-imapConn.LoggedOut()
|
||||||
|
|
||||||
pool.locker.Lock()
|
sm.locker.Lock()
|
||||||
delete(pool.sessions, token)
|
delete(sm.sessions, token)
|
||||||
pool.locker.Unlock()
|
sm.locker.Unlock()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return token, nil
|
return token, nil
|
Loading…
Reference in a new issue