Mercurial > gemma
view auth/connection.go @ 256:1ff9aec9326f
db-setup script: Be more quiet during creation.
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Fri, 27 Jul 2018 12:49:39 +0200 |
parents | 11d1a488b08f |
children | 33b59c848771 |
line wrap: on
line source
package auth import ( "database/sql" "errors" "io" "log" "sync" "time" ) var ErrNoSuchToken = errors.New("No such token") const ( maxOpen = 16 maxDBIdle = time.Minute * 5 ) type Connection struct { session *Session access time.Time db *sql.DB refCount int mu sync.Mutex } func (c *Connection) serialize(w io.Writer) error { if err := c.session.serialize(w); err != nil { return err } access, err := c.last().MarshalText() if err != nil { return err } wr := binWriter{w: w} wr.write(uint32(len(access))) wr.write(access) return wr.err } func (c *Connection) deserialize(r io.Reader) error { session := new(Session) if err := session.deserialize(r); err != nil { return err } rd := binReader{r: r} var l uint32 rd.read(&l) access := make([]byte, l) rd.read(access) if rd.err != nil { return rd.err } var t time.Time if err := t.UnmarshalText(access); err != nil { return err } *c = Connection{ session: session, access: t, } return nil } func (c *Connection) set(session *Session) { c.session = session c.touch() } func (c *Connection) touch() { c.mu.Lock() c.access = time.Now() c.mu.Unlock() } func (c *Connection) last() time.Time { c.mu.Lock() access := c.access c.mu.Unlock() return access } func (c *Connection) close() { if c.db != nil { if err := c.db.Close(); err != nil { log.Printf("warn: %v\n", err) } c.db = nil } }