|
@@ -11,14 +11,55 @@ import (
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"sync"
|
|
|
+ "sync/atomic"
|
|
|
|
|
|
"app.yhyue.com/moapp/jybase/es"
|
|
|
ossSDK "github.com/aliyun/aliyun-oss-go-sdk/oss"
|
|
|
"jygit.jydev.jianyu360.cn/BaseService/ossService/config"
|
|
|
+ "jygit.jydev.jianyu360.cn/BaseService/ossService/util"
|
|
|
)
|
|
|
|
|
|
-// accountMap 用来缓存OSS帐号信息
|
|
|
-var accountMap sync.Map
|
|
|
+var (
|
|
|
+ // accountMap 用来缓存OSS帐号信息
|
|
|
+ accountMap sync.Map
|
|
|
+ DownLoadPool = make(chan bool, g.Config().MustGet(gctx.New(), "downLoadPoolSize").Int())
|
|
|
+ GetDetailFromEsPool = make(chan bool, g.Config().MustGet(gctx.New(), "getDetailFromEsPoolSize").Int())
|
|
|
+ GetDetailFromMgoPool = make(chan bool, g.Config().MustGet(gctx.New(), "getDetailFromMgoPoolSize").Int())
|
|
|
+ getDetail = map[string]func(bucketID, objectName string) []byte{
|
|
|
+ "oss": func(bucketID, objectName string) []byte {
|
|
|
+ b, _, e := DownloadAttachment(bucketID, objectName)
|
|
|
+ if e == nil && b != nil && len(b) > 0 {
|
|
|
+ return b
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ "es": func(bucketID, objectName string) []byte {
|
|
|
+ GetDetailFromEsPool <- true
|
|
|
+ defer func() {
|
|
|
+ <-GetDetailFromEsPool
|
|
|
+ }()
|
|
|
+ indexName := g.Config().MustGet(gctx.New(), "elasticSearch.indexName").String()
|
|
|
+ list := es.VarEs.Get(indexName, indexName, fmt.Sprintf(`{"query":{"bool":{"filter":{"term":{"_id":"%s"}}}},"_source":["detail"]}`, objectName))
|
|
|
+ if list != nil && len(*list) > 0 {
|
|
|
+ detail, _ := (*list)[0]["detail"].(string)
|
|
|
+ return []byte(detail)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ "mgo": func(bucketID, objectName string) []byte {
|
|
|
+ GetDetailFromMgoPool <- true
|
|
|
+ defer func() {
|
|
|
+ <-GetDetailFromMgoPool
|
|
|
+ }()
|
|
|
+ data, _ := config.Mgo.FindOneByField(g.Config().MustGet(gctx.New(), "mongodb.collection").String(), objectName, `{"detail":1}`)
|
|
|
+ if data != nil && len(*data) > 0 {
|
|
|
+ detail, _ := (*data)["detail"].(string)
|
|
|
+ return []byte(detail)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ },
|
|
|
+ }
|
|
|
+)
|
|
|
|
|
|
// GetBucket 根据bucketID获取bucket信息,如果没有则模拟查询数据库(这里只查询一次)
|
|
|
func GetBucket(bucketID string) (config.BucketInfo, error) {
|
|
@@ -106,6 +147,12 @@ func UploadAttachment(bucketID, objectName string, data io.Reader, gzipEnabled b
|
|
|
|
|
|
// DownloadAttachment 下载附件,如果检测到数据为gzip压缩,则自动解压后返回
|
|
|
func DownloadAttachment(bucketID, objectName string) ([]byte, http.Header, error) {
|
|
|
+ atomic.AddInt64(&util.DownLoadCounter, 1)
|
|
|
+ DownLoadPool <- true
|
|
|
+ defer func() {
|
|
|
+ <-DownLoadPool
|
|
|
+ atomic.AddInt64(&util.DownLoadCounter, -1)
|
|
|
+ }()
|
|
|
bucket, err := GetCachedBucket(bucketID)
|
|
|
if err != nil {
|
|
|
return nil, nil, err
|
|
@@ -159,15 +206,13 @@ func LoadOSSAccounts() {
|
|
|
|
|
|
// 获取标讯正文,优先从oss中取,再从es中取
|
|
|
func GetBidDetail(bucketID, objectName string) []byte {
|
|
|
- b, _, e := DownloadAttachment(bucketID, objectName)
|
|
|
- if e == nil && b != nil && len(b) > 0 {
|
|
|
- return b
|
|
|
- }
|
|
|
- indexName := g.Config().MustGet(gctx.New(), "elasticSearch.indexName").String()
|
|
|
- list := es.VarEs.Get(indexName, indexName, fmt.Sprintf(`{"query":{"bool":{"filter":{"term":{"_id":"%s"}}}},"_source":["detail"]}`, objectName))
|
|
|
- if list != nil && len(*list) > 0 {
|
|
|
- detail, _ := (*list)[0]["detail"].(string)
|
|
|
- return []byte(detail)
|
|
|
+ atomic.AddInt64(&util.GetDetailCounter, 1)
|
|
|
+ defer atomic.AddInt64(&util.GetDetailCounter, -1)
|
|
|
+ for _, v := range g.Config().MustGet(gctx.New(), "getDetailOrder").Strings() {
|
|
|
+ detail := getDetail[v](bucketID, objectName)
|
|
|
+ if detail != nil && len(detail) > 0 {
|
|
|
+ return detail
|
|
|
+ }
|
|
|
}
|
|
|
return nil
|
|
|
}
|