keep connections open

This commit is contained in:
Your Name 2021-08-16 19:55:59 -04:00
parent 0e745fe5a1
commit 69f2573e26
1 changed files with 46 additions and 53 deletions

99
main.go
View File

@ -11,9 +11,9 @@ import (
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"log" "log"
"net" "net"
"net/textproto"
"os" "os"
"sort" "sort"
"net/textproto"
"sync" "sync"
"time" "time"
) )
@ -97,10 +97,10 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
object.lock.RUnlock() object.lock.RUnlock()
object.lock.Lock() object.lock.Lock()
object.lastHit=time.Now() object.lastHit = time.Now()
object.lock.Unlock() object.lock.Unlock()
c.lock.Lock() c.lock.Lock()
c.objects[o]=object c.objects[o] = object
c.lock.Unlock() c.lock.Unlock()
if !ok { if !ok {
// Value does not exist! Send it back. // Value does not exist! Send it back.
@ -111,23 +111,23 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
func loadDbEntries(ot string, id int64) map[string]metadataValues { func loadDbEntries(ot string, id int64) map[string]metadataValues {
log.Println("loadDbEntries") log.Println("loadDbEntries")
var entries map[string]metadataValues var entries map[string]metadataValues
entries=make (map[string]metadataValues) entries = make(map[string]metadataValues)
var table string var table string
var column string var column string
if ot=="u" { if ot == "u" {
table="wp_usermeta" table = "wp_usermeta"
column="user_id" column = "user_id"
} else if ot=="p" { } else if ot == "p" {
table="wp_postmeta" table = "wp_postmeta"
column="post_id" column = "post_id"
} else { } else {
log.Printf("Invalid object type: %s", ot) log.Printf("Invalid object type: %s", ot)
return entries return entries
} }
query:=fmt.Sprintf("select meta_key, meta_value from %s where %s = ?", table, column) query := fmt.Sprintf("select meta_key, meta_value from %s where %s = ?", table, column)
log.Println(query) log.Println(query)
rows,err:=db.Query(query, id) rows, err := db.Query(query, id)
if err != nil { if err != nil {
log.Printf("db.Query: %s\n", err.Error()) log.Printf("db.Query: %s\n", err.Error())
return entries return entries
@ -137,10 +137,10 @@ func loadDbEntries(ot string, id int64) map[string]metadataValues {
var key string var key string
var value string var value string
rows.Scan(&key, &value) rows.Scan(&key, &value)
values,_:=entries[key] values, _ := entries[key]
values.Values=append(values.Values, value) values.Values = append(values.Values, value)
entries[key]=values entries[key] = values
} }
return entries return entries
} }
@ -164,17 +164,17 @@ func main() {
var UC MetadataCache var UC MetadataCache
UC.objects = make(map[int64]metadataObject) UC.objects = make(map[int64]metadataObject)
UC.sizeLimit = 100 UC.sizeLimit = 100
UC.objectType="u" UC.objectType = "u"
var PC MetadataCache var PC MetadataCache
PC.objects = make(map[int64]metadataObject) PC.objects = make(map[int64]metadataObject)
PC.sizeLimit = 100 PC.sizeLimit = 100
PC.objectType="p" PC.objectType = "p"
if err := os.RemoveAll(*sockpath); err != nil { if err := os.RemoveAll(*sockpath); err != nil {
log.Fatal(err) log.Fatal(err)
} }
unixListener, err := net.Listen("unix", *sockpath) unixListener, err := net.Listen("unix", *sockpath)
if err!=nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
os.Chmod(*sockpath, 0777) os.Chmod(*sockpath, 0777)
@ -196,47 +196,40 @@ type cachecommand struct {
func handleConnection(conn net.Conn, UC MetadataCache, PC MetadataCache) { func handleConnection(conn net.Conn, UC MetadataCache, PC MetadataCache) {
log.Println("handleConnection started") log.Println("handleConnection started")
var buf string
var m *MetadataCache var m *MetadataCache
reader := bufio.NewReader(conn) reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader) tp := textproto.NewReader(reader)
buf,err:=tp.ReadLine() for {
if err != nil { buf, err := tp.ReadLine()
log.Printf("tp.ReadLine returned %s\n", err.Error())
return
}
var c cachecommand
err = json.Unmarshal([]byte(buf), &c)
if err != nil {
log.Printf("json.Unmarshal: %s returned %s", buf, err.Error())
}
log.Printf("JSON got: %#v", c)
var values []string
if c.ObjectType == "u" {
m = &UC
} else if c.ObjectType == "p" {
m = &PC
}
if c.Command == "g" {
values, err = m.Get(c.ObjectId, c.Key)
if err != nil { if err != nil {
conn.Write([]byte("404"))
conn.Close() conn.Close()
return return
} }
p, err := phpserialize.Marshal(values, nil) var c cachecommand
err = json.Unmarshal([]byte(buf), &c)
if err != nil { if err != nil {
log.Fatalf("phpserialize.Marshal: %s", err.Error()) log.Printf("json.Unmarshal: %s returned %s", buf, err.Error())
}
var values []string
if c.ObjectType == "u" {
m = &UC
} else if c.ObjectType == "p" {
m = &PC
}
if c.Command == "g" {
values, err = m.Get(c.ObjectId, c.Key)
if err != nil {
conn.Write([]byte("404"))
} else {
p, err := phpserialize.Marshal(values, nil)
if err != nil {
log.Fatalf("phpserialize.Marshal: %s", err.Error())
}
conn.Write(p)
}
} else if c.Command == "d" {
m.Delete(c.ObjectId)
conn.Write([]byte("200"))
} }
conn.Write(p)
conn.Close()
return
} }
if c.Command=="d" {
m.Delete(c.ObjectId)
conn.Write([]byte("200"))
conn.Close()
return
}
} }