This commit is contained in:
2020-04-15 09:42:50 -04:00
parent 91f0c9b1f9
commit 5cd367456c
12 changed files with 273 additions and 166 deletions

View File

@@ -1,16 +1,12 @@
package main
package api
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v7"
"github.com/mailru/easyjson"
. "github.com/simon987/imhashdb"
"log"
"os"
"runtime/pprof"
"time"
)
@@ -112,28 +108,15 @@ func hash(c *gin.Context) {
c.Data(200, gin.MIMEJSON, b)
}
func main() {
Init()
f, err := os.Create("prof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
go func() {
time.Sleep(time.Second * 15)
pprof.StopCPUProfile()
fmt.Println("!!!!!!!!!!!!!!!")
f.Close()
}()
func Main() error {
r := gin.Default()
r.Use(CORSMiddleware())
r.POST("/api/hash", hash)
r.POST("/api/query", query)
//TODO: concurrency
go queryWorker()
for i := 0; i < Conf.QueryConcurrency; i++ {
go queryWorker()
}
r.Run()
return r.Run(Conf.ApiAddr)
}

View File

@@ -1,4 +1,4 @@
package main
package api
import (
"context"
@@ -17,7 +17,7 @@ const CacheLength = time.Second * 30
func queryWorker() {
Logger.Info("Query worker started")
for {
value := Rdb.BZPopMin(time.Second * 30, inQueue).Val()
value := Rdb.BZPopMin(time.Second*30, inQueue).Val()
if value == nil {
continue
}
@@ -38,13 +38,13 @@ func queryWorker() {
Logger.Info("worker query done")
b = resp
}
Rdb.Set(outQueue + member, b, CacheLength)
Rdb.Set(outQueue+member, b, CacheLength)
}
}
func dbQuery(req QueryReq, value string) ([]byte, error) {
Rdb.SAdd(wipQueue, value)
Rdb.Expire(wipQueue, time.Minute * 10)
Rdb.Expire(wipQueue, time.Minute*10)
defer Rdb.SRem(wipQueue, value)