From 8a2a98726d0b7700e1796fb114a2af1451d2dbee Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 13 Aug 2021 19:16:30 -0300 Subject: [PATCH] initialize the server when making it instead of during request --- server/search.go | 20 +------------------- server/server.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/server/search.go b/server/search.go index 6487c03..8873374 100644 --- a/server/search.go +++ b/server/search.go @@ -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) diff --git a/server/server.go b/server/server.go index 167d14c..14f752a 100644 --- a/server/server.go +++ b/server/server.go @@ -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