# HG changeset patch # User Sascha L. Teichmann # Date 1535113505 -7200 # Node ID 8a0737aa6ab60f25e95e6b12de5975e616638a92 # Parent b2dc9c2f69e0bfef262b846d35438ee324e8769e The connection pool is now only a session store. diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 cmd/gemma/main.go --- a/cmd/gemma/main.go Fri Aug 24 13:56:06 2018 +0200 +++ b/cmd/gemma/main.go Fri Aug 24 14:25:05 2018 +0200 @@ -19,13 +19,13 @@ "gemma.intevation.de/gemma/pkg/controllers" ) -func prepareConnectionPool() { - // Install connection pool - cp, err := auth.NewConnectionPool(config.SessionStore()) +func prepareSessionStore() { + // Install session store + ss, err := auth.NewSessionStore(config.SessionStore()) if err != nil { log.Fatalf("Error with session store: %v\n", err) } - auth.ConnPool = cp + auth.Sessions = ss } func start(cmd *cobra.Command, args []string) { @@ -35,7 +35,7 @@ log.Fatalf("error: %v\n", err) } - prepareConnectionPool() + prepareSessionStore() // Do GeoServer setup in background. go func() { @@ -88,7 +88,7 @@ <-done - if err := auth.ConnPool.Shutdown(); err != nil { + if err := auth.Sessions.Shutdown(); err != nil { log.Fatalf("error: %v\n", err) } } diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/auth/connection.go --- a/pkg/auth/connection.go Fri Aug 24 13:56:06 2018 +0200 +++ b/pkg/auth/connection.go Fri Aug 24 14:25:05 2018 +0200 @@ -11,11 +11,6 @@ var ErrNoSuchToken = errors.New("No such token") -const ( - maxOpen = 16 - maxDBIdle = time.Minute * 5 -) - type Connection struct { session *Session @@ -84,6 +79,3 @@ c.mu.Unlock() return access } - -func (c *Connection) close() { -} diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/auth/middleware.go --- a/pkg/auth/middleware.go Fri Aug 24 13:56:06 2018 +0200 +++ b/pkg/auth/middleware.go Fri Aug 24 14:25:05 2018 +0200 @@ -35,7 +35,7 @@ return } - session := ConnPool.Session(token) + session := Sessions.Session(token) if session == nil { http.Error(rw, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/auth/pool.go --- 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 diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/auth/session.go --- a/pkg/auth/session.go Fri Aug 24 13:56:06 2018 +0200 +++ b/pkg/auth/session.go Fri Aug 24 14:25:05 2018 +0200 @@ -96,6 +96,6 @@ } token := GenerateSessionKey() session := NewSession(user, password, roles) - ConnPool.Add(token, session) + Sessions.Add(token, session) return token, session, nil } diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/controllers/json.go --- a/pkg/controllers/json.go Fri Aug 24 13:56:06 2018 +0200 +++ b/pkg/controllers/json.go Fri Aug 24 14:25:05 2018 +0200 @@ -49,7 +49,7 @@ if token, ok := auth.GetToken(req); ok && !j.NoConn { var session *auth.Session - if session, err = auth.ConnPool.Do(token); err != nil { + if session, err = auth.Sessions.Do(token); err != nil { var conn *sql.Conn if conn, err = auth.MetamorphConn(req.Context(), session.User); err != nil { defer conn.Close() diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/controllers/token.go --- a/pkg/controllers/token.go Fri Aug 24 13:56:06 2018 +0200 +++ b/pkg/controllers/token.go Fri Aug 24 14:25:05 2018 +0200 @@ -19,7 +19,7 @@ func renew(rw http.ResponseWriter, req *http.Request) { token, _ := auth.GetToken(req) - newToken, err := auth.ConnPool.Renew(token) + newToken, err := auth.Sessions.Renew(token) switch { case err == auth.ErrNoSuchToken: http.NotFound(rw, req) @@ -48,7 +48,7 @@ func logout(rw http.ResponseWriter, req *http.Request) { token, ok := auth.GetToken(req) - if !ok || !auth.ConnPool.Delete(token) { + if !ok || !auth.Sessions.Delete(token) { http.NotFound(rw, req) return } diff -r b2dc9c2f69e0 -r 8a0737aa6ab6 pkg/controllers/user.go --- a/pkg/controllers/user.go Fri Aug 24 13:56:06 2018 +0200 +++ b/pkg/controllers/user.go Fri Aug 24 14:25:05 2018 +0200 @@ -84,7 +84,7 @@ } // Running in a go routine should not be necessary. - go func() { auth.ConnPool.Logout(user) }() + go func() { auth.Sessions.Logout(user) }() jr = JSONResult{Code: http.StatusNoContent} return @@ -161,7 +161,7 @@ if user != newUser.User { // Running in a go routine should not be necessary. - go func() { auth.ConnPool.Logout(string(user)) }() + go func() { auth.Sessions.Logout(string(user)) }() } jr = JSONResult{