瀏覽代碼

wip:自测提交

wangkaiyue 2 年之前
父節點
當前提交
0723b8b7b6
共有 2 個文件被更改,包括 16 次插入7 次删除
  1. 3 3
      config.yaml
  2. 13 4
      internal/service/manager.go

+ 3 - 3
config.yaml

@@ -8,9 +8,9 @@ elasticSearch:
   address:
     - "http://192.168.3.241:9205"
   pool:
-    simple: 20
-    aggs: 10
-    complex: 10
+    simple: 5
+    aggs: 2
+    complex: 2
   waitTime: 5 #单位秒
   complexQueryLen: 200 #查询条件复杂长度
 

+ 13 - 4
internal/service/manager.go

@@ -9,7 +9,6 @@ import (
 	"net/http/httputil"
 	"net/url"
 	"os"
-	"sync"
 	"time"
 )
 
@@ -18,7 +17,6 @@ var (
 )
 
 type esProxyManager struct {
-	lock                                             sync.Mutex
 	esStatus                                         consts.EsStatus
 	proxyPool                                        chan *httputil.ReverseProxy
 	simpleQueryPool, aggsQueryPool, complexQueryPool chan struct{}
@@ -42,7 +40,6 @@ func initManager(ctx context.Context) *esProxyManager {
 	}
 
 	emp := &esProxyManager{
-		lock:             sync.Mutex{},
 		esStatus:         consts.EsStatus_Free,
 		proxyPool:        pool,
 		simpleQueryPool:  makeEmptyChan(g.Cfg().MustGet(ctx, "elasticSearch.pool.simple", 10).Int()),
@@ -83,6 +80,7 @@ func (epm *esProxyManager) GetProxy(ctx context.Context, queryLevel int) (*httpu
 	if queryLevel+int(epm.esStatus) > 2 {
 		return nil, fmt.Errorf("server is busy")
 	}
+	//epm.Status()
 	select {
 	case <-epm.getPool(queryLevel):
 		c := <-epm.proxyPool
@@ -95,7 +93,10 @@ func (epm *esProxyManager) GetProxy(ctx context.Context, queryLevel int) (*httpu
 
 // Release 归还链接池
 func (epm *esProxyManager) Release(ctx context.Context, queryLevel int) {
-	epm.getPool(queryLevel) <- struct{}{}
+	go func() {
+		time.Sleep(time.Second * 10)
+		epm.getPool(queryLevel) <- struct{}{}
+	}()
 }
 
 // 获取对应查询复杂度的链接池
@@ -109,3 +110,11 @@ func (epm *esProxyManager) getPool(queryLevel int) chan struct{} {
 		return epm.complexQueryPool
 	}
 }
+
+func (epm *esProxyManager) Status() {
+	g.Dump(map[string]interface{}{
+		"simple":  len(epm.simpleQueryPool),
+		"aggs":    len(epm.aggsQueryPool),
+		"complex": len(epm.complexQueryPool),
+	})
+}