initialize the server when making it instead of during request

This commit is contained in:
Victor Shyba 2021-08-13 19:16:30 -03:00
parent b4782ce6ac
commit 8a2a98726d
2 changed files with 12 additions and 19 deletions

View file

@ -8,7 +8,6 @@ import (
"fmt"
"log"
"math"
"os"
"reflect"
"strings"
"time"
@ -144,28 +143,11 @@ func AddInvertibleField(q *elastic.BoolQuery, field *pb.InvertibleField, name st
// 8) return streams referenced by repost and all channel referenced in extra_txos
//*/
func (s *Server) Search(ctx context.Context, in *pb.SearchRequest) (*pb.Outputs, error) {
var client *elastic.Client = nil
if s.EsClient == nil {
esUrl := s.Args.EsHost + ":" + s.Args.EsPort
opts := []elastic.ClientOptionFunc{elastic.SetSniff(false), elastic.SetURL(esUrl)}
if s.Args.Debug {
opts = append(opts, elastic.SetTraceLog(log.New(os.Stderr, "[[ELASTIC]]", 0)))
}
tmpClient, err := elastic.NewClient(opts...)
if err != nil {
log.Println(err)
return nil, err
}
client = tmpClient
s.EsClient = client
} else {
client = s.EsClient
}
var from = 0
var pageSize = 10
var orderBy []orderField
var searchIndices = []string{}
client := s.EsClient
searchIndices = make([]string, 0, 1)
searchIndices = append(searchIndices, s.Args.EsIndex)

View file

@ -2,6 +2,7 @@ package server
import (
"log"
"os"
"regexp"
pb "github.com/lbryio/hub/protobuf/go"
@ -80,11 +81,21 @@ func MakeHubServer(args *Args) *Server {
log.Fatal(err)
}
esUrl := args.EsHost + ":" + args.EsPort
opts := []elastic.ClientOptionFunc{elastic.SetSniff(false), elastic.SetURL(esUrl)}
if args.Debug {
opts = append(opts, elastic.SetTraceLog(log.New(os.Stderr, "[[ELASTIC]]", 0)))
}
client, err := elastic.NewClient(opts...)
if err != nil {
log.Fatal(err)
}
s := &Server{
GrpcServer: grpcServer,
Args: args,
MultiSpaceRe: multiSpaceRe,
WeirdCharsRe: weirdCharsRe,
EsClient: client,
}
return s