fork of https://github.com/sourcegraph/zoekt
1package srv
2
3import (
4 "context"
5 "time"
6
7 "github.com/sourcegraph/zoekt"
8 "github.com/sourcegraph/zoekt/query"
9)
10
11// defaultTimeout is the maximum amount of time a search request should
12// take. This is the same default used by Sourcegraph.
13const defaultTimeout = 20 * time.Second
14
15type SearchArgs struct {
16 Q query.Q
17 Opts *zoekt.SearchOptions
18}
19
20type SearchReply struct {
21 Result *zoekt.SearchResult
22}
23
24type ListArgs struct {
25 Q query.Q
26 Opts *zoekt.ListOptions
27}
28
29type ListReply struct {
30 List *zoekt.RepoList
31}
32
33type Searcher struct {
34 Searcher zoekt.Searcher
35}
36
37func (s *Searcher) Search(ctx context.Context, args *SearchArgs, reply *SearchReply) error {
38 // Set a timeout if the user hasn't specified one.
39 if args.Opts != nil && args.Opts.MaxWallTime == 0 {
40 var cancel context.CancelFunc
41 ctx, cancel = context.WithTimeout(ctx, defaultTimeout)
42 defer cancel()
43 }
44
45 if args.Q != nil {
46 args.Q = query.RPCUnwrap(args.Q)
47 }
48
49 r, err := s.Searcher.Search(ctx, args.Q, args.Opts)
50 if err != nil {
51 return err
52 }
53 reply.Result = r
54 return nil
55}
56
57func (s *Searcher) List(ctx context.Context, args *ListArgs, reply *ListReply) error {
58 ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
59 defer cancel()
60
61 if args.Q != nil {
62 args.Q = query.RPCUnwrap(args.Q)
63 }
64
65 r, err := s.Searcher.List(ctx, args.Q, args.Opts)
66 if err != nil {
67 return err
68 }
69 reply.List = r
70 return nil
71}