diff pkg/auth/pool.go @ 493:8a0737aa6ab6 metamorph-for-all

The connection pool is now only a session store.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 24 Aug 2018 14:25:05 +0200
parents b2dc9c2f69e0
children
line wrap: on
line diff
--- a/pkg/auth/pool.go	Fri Aug 24 13:56:06 2018 +0200
+++ b/pkg/auth/pool.go	Fri Aug 24 14:25:05 2018 +0200
@@ -8,22 +8,22 @@
 	bolt "github.com/coreos/bbolt"
 )
 
-// ConnPool is the global connection pool.
-var ConnPool *ConnectionPool
+// Sessions is the global connection pool.
+var Sessions *SessionStore
 
-type ConnectionPool struct {
+type SessionStore struct {
 	storage *bolt.DB
 	conns   map[string]*Connection
-	cmds    chan func(*ConnectionPool)
+	cmds    chan func(*SessionStore)
 }
 
 var sessionsBucket = []byte("sessions")
 
-func NewConnectionPool(filename string) (*ConnectionPool, error) {
+func NewSessionStore(filename string) (*SessionStore, error) {
 
-	pcp := &ConnectionPool{
+	pcp := &SessionStore{
 		conns: make(map[string]*Connection),
-		cmds:  make(chan func(*ConnectionPool)),
+		cmds:  make(chan func(*SessionStore)),
 	}
 	if err := pcp.openStorage(filename); err != nil {
 		return nil, err
@@ -33,7 +33,7 @@
 }
 
 // openStorage opens a storage file.
-func (pcp *ConnectionPool) openStorage(filename string) error {
+func (pcp *SessionStore) openStorage(filename string) error {
 
 	// No file, nothing to restore/persist.
 	if filename == "" {
@@ -74,36 +74,29 @@
 	return nil
 }
 
-func (pcp *ConnectionPool) run() {
+func (pcp *SessionStore) run() {
 	for {
 		select {
 		case cmd := <-pcp.cmds:
 			cmd(pcp)
-		case <-time.After(time.Minute):
-			pcp.cleanDB()
 		case <-time.After(time.Minute * 5):
 			pcp.cleanToken()
 		}
 	}
 }
 
-func (pcp *ConnectionPool) cleanDB() {
-}
-
-func (pcp *ConnectionPool) cleanToken() {
+func (pcp *SessionStore) cleanToken() {
 	now := time.Now()
 	for token, con := range pcp.conns {
 		expires := time.Unix(con.session.ExpiresAt, 0)
 		if expires.Before(now) {
-			// TODO: Be more graceful here?
-			con.close()
 			delete(pcp.conns, token)
 			pcp.remove(token)
 		}
 	}
 }
 
-func (pcp *ConnectionPool) remove(token string) {
+func (pcp *SessionStore) remove(token string) {
 	if pcp.storage == nil {
 		return
 	}
@@ -116,15 +109,13 @@
 	}
 }
 
-func (pcp *ConnectionPool) Delete(token string) bool {
+func (pcp *SessionStore) Delete(token string) bool {
 	res := make(chan bool)
-	pcp.cmds <- func(pcp *ConnectionPool) {
-		conn, found := pcp.conns[token]
-		if !found {
+	pcp.cmds <- func(pcp *SessionStore) {
+		if _, found := pcp.conns[token]; !found {
 			res <- false
 			return
 		}
-		conn.close()
 		delete(pcp.conns, token)
 		pcp.remove(token)
 		res <- true
@@ -132,7 +123,7 @@
 	return <-res
 }
 
-func (pcp *ConnectionPool) store(token string, con *Connection) {
+func (pcp *SessionStore) store(token string, con *Connection) {
 	if pcp.storage == nil {
 		return
 	}
@@ -149,10 +140,10 @@
 	}
 }
 
-func (pcp *ConnectionPool) Add(token string, session *Session) *Connection {
+func (pcp *SessionStore) Add(token string, session *Session) *Connection {
 	res := make(chan *Connection)
 
-	pcp.cmds <- func(cp *ConnectionPool) {
+	pcp.cmds <- func(cp *SessionStore) {
 		con := pcp.conns[token]
 		if con == nil {
 			con = &Connection{}
@@ -167,7 +158,7 @@
 	return con
 }
 
-func (pcp *ConnectionPool) Renew(token string) (string, error) {
+func (pcp *SessionStore) Renew(token string) (string, error) {
 
 	type result struct {
 		newToken string
@@ -176,7 +167,7 @@
 
 	resCh := make(chan result)
 
-	pcp.cmds <- func(cp *ConnectionPool) {
+	pcp.cmds <- func(cp *SessionStore) {
 		con := pcp.conns[token]
 		if con == nil {
 			resCh <- result{err: ErrNoSuchToken}
@@ -196,7 +187,7 @@
 	return r.newToken, r.err
 }
 
-func (pcp *ConnectionPool) Do(token string) (*Session, error) {
+func (pcp *SessionStore) Do(token string) (*Session, error) {
 
 	type result struct {
 		session *Session
@@ -205,7 +196,7 @@
 
 	res := make(chan result)
 
-	pcp.cmds <- func(pcp *ConnectionPool) {
+	pcp.cmds <- func(pcp *SessionStore) {
 		con := pcp.conns[token]
 		if con == nil {
 			res <- result{err: ErrNoSuchToken}
@@ -226,9 +217,9 @@
 	return r.session, nil
 }
 
-func (pcp *ConnectionPool) Session(token string) *Session {
+func (pcp *SessionStore) Session(token string) *Session {
 	res := make(chan *Session)
-	pcp.cmds <- func(pcp *ConnectionPool) {
+	pcp.cmds <- func(pcp *SessionStore) {
 		con := pcp.conns[token]
 		if con == nil {
 			res <- nil
@@ -241,8 +232,8 @@
 	return <-res
 }
 
-func (pcp *ConnectionPool) Logout(user string) {
-	pcp.cmds <- func(pcp *ConnectionPool) {
+func (pcp *SessionStore) Logout(user string) {
+	pcp.cmds <- func(pcp *SessionStore) {
 		for token, con := range pcp.conns {
 			if con.session.User == user {
 				delete(pcp.conns, token)
@@ -252,7 +243,7 @@
 	}
 }
 
-func (pcp *ConnectionPool) Shutdown() error {
+func (pcp *SessionStore) Shutdown() error {
 	if db := pcp.storage; db != nil {
 		log.Println("info: shutdown persistent connection pool.")
 		pcp.storage = nil