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