/
goroutinesearcher.go
172 lines (147 loc) · 4.25 KB
/
goroutinesearcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// HipparchiaGoServer
// Copyright: E Gunderson 2022
// License: GNU GENERAL PUBLIC LICENSE 3
// (see LICENSE in the top level directory of the distribution)
package main
import (
"context"
"github.com/gorilla/websocket"
"log"
"sync"
)
var (
upgrader = websocket.Upgrader{}
)
//
// THE MANAGER
//
// HGoSrch - the core of a search: coordingates the dispatch of queries and collection of results
func HGoSrch(ss SearchStruct) SearchStruct {
// NOTE: this is all much more "go-like" than HipparchiaGolangSearcher() in grabber.go,
// BUT python + redis + HipparchiaGolangSearcher() is marginally faster than what follows [channels produce overhead?]
// see https://go.dev/blog/pipelines : see Parallel digestion & Fan-out, fan-in & Explicit cancellation
// https://medium.com/amboss/applying-modern-go-concurrency-patterns-to-data-pipelines-b3b5327908d4
// https://github.com/amboss-mededu/go-pipeline-article/blob/fe0cebe78ecc9c57cdb1ac83ae6af1cda44de475/main.go
// https://itnext.io/golang-pipeline-in-practise-6007dafbb85f
// https://medium.com/geekculture/golang-concurrency-patterns-fan-in-fan-out-1ee43c6830c4
// https://pranav93.github.io/blog/golang-fan-inout-pattern/
// https://github.com/luk4z7/go-concurrency-guide
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
emitqueries, err := SrchFeeder(ctx, &ss)
chke(err)
workers := Config.WorkerCount
findchannels := make([]<-chan []DbWorkline, workers)
for i := 0; i < workers; i++ {
fc, e := SrchConsumer(ctx, emitqueries)
chke(e)
findchannels[i] = fc
}
max := ss.CurrentLimit
if ss.HasPhrase {
// windowing double-hits; c. 55% are valid; these get pared via findphrasesacrosslines()
max = ss.CurrentLimit * 3
}
results := ResultCollation(ctx, &ss, max, ResultAggregator(ctx, findchannels...))
if int64(len(results)) > max {
results = results[0:max]
}
ss.Results = results
return ss
}
//
// THE WORKERS
//
// SrchFeeder - emit items to a channel from the []PrerolledQuery that will be consumed by the SrchConsumer
func SrchFeeder(ctx context.Context, ss *SearchStruct) (<-chan PrerolledQuery, error) {
emitqueries := make(chan PrerolledQuery, Config.WorkerCount)
remainder := -1
go func() {
defer close(emitqueries)
for i, q := range ss.Queries {
select {
case <-ctx.Done():
break
default:
remainder = len(ss.Queries) - i - 1
if remainder%POLLEVERYNTABLES == 0 {
ss.Remain.Set(remainder)
}
emitqueries <- q
}
}
}()
return emitqueries, nil
}
// SrchConsumer - grab a PrerolledQuery; execute search; emit finds to a channel
func SrchConsumer(ctx context.Context, prq <-chan PrerolledQuery) (<-chan []DbWorkline, error) {
emitfinds := make(chan []DbWorkline)
go func() {
dbconn := GetPSQLconnection()
defer dbconn.Release()
defer close(emitfinds)
for q := range prq {
select {
case <-ctx.Done():
return
default:
emitfinds <- worklinequery(q, dbconn)
}
}
}()
return emitfinds, nil
}
// ResultAggregator - gather all hits from the findchannels into one place and then feed them to ResultCollation
func ResultAggregator(ctx context.Context, findchannels ...<-chan []DbWorkline) <-chan []DbWorkline {
var wg sync.WaitGroup
emitaggregate := make(chan []DbWorkline)
broadcast := func(ll <-chan []DbWorkline) {
defer wg.Done()
for l := range ll {
select {
case emitaggregate <- l:
case <-ctx.Done():
return
}
}
}
wg.Add(len(findchannels))
for _, fc := range findchannels {
go broadcast(fc)
}
go func() {
wg.Wait()
close(emitaggregate)
}()
return emitaggregate
}
// ResultCollation - return the actual []DbWorkline results after pulling them from the ResultAggregator channel
func ResultCollation(ctx context.Context, ss *SearchStruct, maxhits int64, values <-chan []DbWorkline) []DbWorkline {
var allhits []DbWorkline
done := false
for {
if done {
break
}
select {
case <-ctx.Done():
log.Print(ctx.Err().Error())
done = true
case val, ok := <-values:
if ok {
if ss.OneHit && ss.PhaseNum == 1 && len(val) > 0 {
allhits = append(allhits, val[0])
} else {
allhits = append(allhits, val...)
}
ss.Hits.Set(len(allhits))
if int64(len(allhits)) > maxhits {
done = true
}
} else {
done = true
}
}
}
return allhits
}