keep connections open
This commit is contained in:
parent
0e745fe5a1
commit
69f2573e26
55
main.go
55
main.go
|
@ -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.
|
||||
|
@ -112,22 +112,22 @@ 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)
|
||||
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,9 +137,9 @@ 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,13 +196,13 @@ 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()
|
||||
for {
|
||||
buf, err := tp.ReadLine()
|
||||
if err != nil {
|
||||
log.Printf("tp.ReadLine returned %s\n", err.Error())
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
var c cachecommand
|
||||
|
@ -210,7 +210,6 @@ func handleConnection(conn net.Conn, UC MetadataCache, PC MetadataCache) {
|
|||
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
|
||||
|
@ -221,22 +220,16 @@ func handleConnection(conn net.Conn, UC MetadataCache, PC MetadataCache) {
|
|||
values, err = m.Get(c.ObjectId, c.Key)
|
||||
if err != nil {
|
||||
conn.Write([]byte("404"))
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
p, err := phpserialize.Marshal(values, nil)
|
||||
if err != nil {
|
||||
log.Fatalf("phpserialize.Marshal: %s", err.Error())
|
||||
}
|
||||
conn.Write(p)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
if c.Command=="d" {
|
||||
} else if c.Command == "d" {
|
||||
m.Delete(c.ObjectId)
|
||||
conn.Write([]byte("200"))
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue