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" "fmt"
"log" "log"
"math" "math"
"os"
"reflect" "reflect"
"strings" "strings"
"time" "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 // 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) { 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 from = 0
var pageSize = 10 var pageSize = 10
var orderBy []orderField var orderBy []orderField
var searchIndices = []string{} var searchIndices = []string{}
client := s.EsClient
searchIndices = make([]string, 0, 1) searchIndices = make([]string, 0, 1)
searchIndices = append(searchIndices, s.Args.EsIndex) searchIndices = append(searchIndices, s.Args.EsIndex)

View file

@ -2,6 +2,7 @@ package server
import ( import (
"log" "log"
"os"
"regexp" "regexp"
pb "github.com/lbryio/hub/protobuf/go" pb "github.com/lbryio/hub/protobuf/go"
@ -80,11 +81,21 @@ func MakeHubServer(args *Args) *Server {
log.Fatal(err) 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{ s := &Server{
GrpcServer: grpcServer, GrpcServer: grpcServer,
Args: args, Args: args,
MultiSpaceRe: multiSpaceRe, MultiSpaceRe: multiSpaceRe,
WeirdCharsRe: weirdCharsRe, WeirdCharsRe: weirdCharsRe,
EsClient: client,
} }
return s return s