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"
"log"
"net"
"net/textproto"
"os"
"sort"
"net/textproto"
"sync"
"time"
)
@ -97,10 +97,10 @@ func (c *MetadataCache) Get(o int64, k string) ([]string, error) {
object.lock.RUnlock()
object.lock.Lock()
object.lastHit=time.Now()
object.lastHit = time.Now()
object.lock.Unlock()
c.lock.Lock()
c.objects[o]=object
c.objects[o] = object
c.lock.Unlock()
if !ok {
// 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 {
log.Println("loadDbEntries")
var entries map[string]metadataValues
entries=make (map[string]metadataValues)
var entries map[string]metadataValues
entries = make(map[string]metadataValues)
var table string
var column string
if ot=="u" {
table="wp_usermeta"
column="user_id"
} else if ot=="p" {
table="wp_postmeta"
column="post_id"
if ot == "u" {
table = "wp_usermeta"
column = "user_id"
} else if ot == "p" {
table = "wp_postmeta"
column = "post_id"
} else {
log.Printf("Invalid object type: %s", ot)
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)
rows,err:=db.Query(query, id)
rows, err := db.Query(query, id)
if err != nil {
log.Printf("db.Query: %s\n", err.Error())
return entries
@ -137,10 +137,10 @@ func loadDbEntries(ot string, id int64) map[string]metadataValues {
var key string
var value string
rows.Scan(&key, &value)
values,_:=entries[key]
values.Values=append(values.Values, value)
entries[key]=values
}
values, _ := entries[key]
values.Values = append(values.Values, value)
entries[key] = values
}
return entries
}
@ -164,17 +164,17 @@ func main() {
var UC MetadataCache
UC.objects = make(map[int64]metadataObject)
UC.sizeLimit = 100
UC.objectType="u"
UC.objectType = "u"
var PC MetadataCache
PC.objects = make(map[int64]metadataObject)
PC.sizeLimit = 100
PC.objectType="p"
PC.objectType = "p"
if err := os.RemoveAll(*sockpath); err != nil {
log.Fatal(err)
}
unixListener, err := net.Listen("unix", *sockpath)
if err!=nil {
if err != nil {
log.Fatal(err)
}
os.Chmod(*sockpath, 0777)
@ -196,47 +196,40 @@ type cachecommand struct {
func handleConnection(conn net.Conn, UC MetadataCache, PC MetadataCache) {
log.Println("handleConnection started")
var buf string
var m *MetadataCache
reader := bufio.NewReader(conn)
tp := textproto.NewReader(reader)
buf,err:=tp.ReadLine()
if err != nil {
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)
for {
buf, err := tp.ReadLine()
if err != nil {
conn.Write([]byte("404"))
conn.Close()
return
}
p, err := phpserialize.Marshal(values, nil)
var c cachecommand
err = json.Unmarshal([]byte(buf), &c)
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
}
}