Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres gob #7

Merged
merged 4 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions cache/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cache
import (
"bytes"
"database/sql"
"encoding/binary"
"encoding/gob"

"fmt"

Expand Down Expand Up @@ -103,7 +103,8 @@ func (c *PostgresDataCache) GetDomain(key string) (*Domain, error) {
b, err := c.lru.Get([]byte(key))
if err == nil {
buf := bytes.NewReader(b)
err = binary.Read(buf, binary.BigEndian, &d)
dec := gob.NewDecoder(buf)
err = dec.Decode(&d)
if err != nil {
panic(err)
}
Expand All @@ -118,8 +119,9 @@ func (c *PostgresDataCache) GetDomain(key string) (*Domain, error) {

d.Domain = domain

buf := &bytes.Buffer{}
err = binary.Write(buf, binary.BigEndian, &d)
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
enc.Encode(&d)
if err != nil {
panic(err)
}
Expand All @@ -135,23 +137,25 @@ func (c *PostgresDataCache) GetAccount(key string) (*Account, error) {
b, err := c.lru.Get([]byte(key))
if err == nil {
buf := bytes.NewReader(b)
err = binary.Read(buf, binary.BigEndian, &account)
dec := gob.NewDecoder(buf)
err = dec.Decode(&account)
if err != nil {
panic(err)
}
return &account, nil
}

err = c.db.QueryRow("SELECT domain FROM domains_domain where domain = $1 LIMIT 1", key).Scan(&id)
err = c.db.QueryRow("SELECT uuid FROM accounts_account where uuid = $1 LIMIT 1", key).Scan(&id)
if err != nil {
/* TODO -- We should store failed attempts in the LRU as well to stop from hitting to DB */
return nil, err
}

account.ID = id

buf := &bytes.Buffer{}
err = binary.Write(buf, binary.BigEndian, &account)
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
err = enc.Encode(&account)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pbs_light.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func auction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
_, err = dataCache.GetAccount(pbs_req.AccountID)
if err != nil {
glog.Info("Invalid account id: ", err)
writeAuctionError(w, "Unknown account id", err)
writeAuctionError(w, "Unknown account id", fmt.Errorf("Unknown account"))
mErrorMeter.Mark(1)
return
}
Expand Down