Skip to content

Commit 03534f1

Browse files
committed
add context to gnverifier methods (close #91)
1 parent 284c870 commit 03534f1

File tree

7 files changed

+30
-16
lines changed

7 files changed

+30
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [v0.9.0] - 2022-03-13 Sun
4+
5+
- Add [#91]: provide context to verification and searh methods.
6+
37
## [v0.8.2] - 2022-02-25 Fri
48

59
- Fix: go.mod bug

gnverifier.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,12 @@ func (gnv gnverifier) VerifyOne(name string) (vlib.Name, error) {
7272

7373
// VerifyBatch takes a list of name-strings, verifies them and returns
7474
// a batch of results back.
75-
func (gnv gnverifier) VerifyBatch(nameStrings []string) []vlib.Name {
75+
func (gnv gnverifier) VerifyBatch(
76+
ctx context.Context,
77+
nameStrings []string,
78+
) []vlib.Name {
7679
params := gnv.setParams(nameStrings)
77-
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
80+
ctx, cancel := context.WithTimeout(ctx, 20*time.Second)
7881
defer cancel()
7982

8083
return gnv.verifier.Verify(ctx, params).Names
@@ -84,26 +87,27 @@ func (gnv gnverifier) VerifyBatch(nameStrings []string) []vlib.Name {
8487
// channel and sends results of verification via output
8588
// channel.
8689
func (gnv gnverifier) VerifyStream(
90+
ctx context.Context,
8791
in <-chan []string,
8892
out chan []vlib.Name,
8993
) {
9094
var wg sync.WaitGroup
9195
wg.Add(gnv.cfg.Jobs)
9296

93-
ctx, cancel := context.WithCancel(context.Background())
97+
ctx, cancel := context.WithCancel(ctx)
9498
defer cancel()
9599

96100
vwChan := gnv.loadNames(ctx, in)
97101

98102
for i := 0; i < gnv.cfg.Jobs; i++ {
99-
go gnv.VerifyWorker(ctx, vwChan, out, &wg)
103+
go gnv.verifyWorker(ctx, vwChan, out, &wg)
100104
}
101105

102106
wg.Wait()
103107
close(out)
104108
}
105109

106-
func (gnv gnverifier) VerifyWorker(
110+
func (gnv gnverifier) verifyWorker(
107111
ctx context.Context,
108112
in <-chan vlib.Input,
109113
out chan<- []vlib.Name,
@@ -126,9 +130,10 @@ func (gnv gnverifier) VerifyWorker(
126130
}
127131

128132
func (gnv gnverifier) Search(
133+
ctx context.Context,
129134
inp search.Input,
130135
) ([]vlib.Name, error) {
131-
res, err := gnv.verifier.Search(context.Background(), inp)
136+
res, err := gnv.verifier.Search(ctx, inp)
132137
return res.Names, err
133138
}
134139

gnverifier/cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33

44
import (
55
"bufio"
6+
"context"
67
_ "embed"
78
"fmt"
89
"io"
@@ -393,7 +394,7 @@ func verifyFile(gnv gnverifier.GNverifier, f io.Reader) {
393394
out := make(chan []vlib.Name)
394395
var wg sync.WaitGroup
395396
wg.Add(1)
396-
go gnv.VerifyStream(in, out)
397+
go gnv.VerifyStream(context.Background(), in, out)
397398
go processResults(gnv, out, &wg)
398399
sc := bufio.NewScanner(f)
399400
names := make([]string, 0, batch)
@@ -460,7 +461,7 @@ func searchQuery(gnv gnverifier.GNverifier, s string) {
460461
if all := gnv.Config().WithAllMatches; all {
461462
inp.WithAllMatches = all
462463
}
463-
res, err := gnv.Search(inp)
464+
res, err := gnv.Search(context.Background(), inp)
464465
if err != nil {
465466
log.Fatal().Err(err)
466467
}

gnverifier_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gnverifier_test
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
@@ -98,7 +99,7 @@ func TestVerifyBatch(t *testing.T) {
9899
"Bubo bubo (Linnaeus, 1782)",
99100
"NotName",
100101
}
101-
res := gnv.VerifyBatch(batch)
102+
res := gnv.VerifyBatch(context.Background(), batch)
102103
assert.Equal(t, 3, len(res))
103104
assert.Equal(t, 1, vfr.VerifyCallCount())
104105
}
@@ -120,7 +121,7 @@ func TestVerifyStream(t *testing.T) {
120121
chOut := make(chan []vlib.Name)
121122
var wg sync.WaitGroup
122123
wg.Add(1)
123-
go gnv.VerifyStream(chIn, chOut)
124+
go gnv.VerifyStream(context.Background(), chIn, chOut)
124125

125126
go func() {
126127
defer wg.Done()

interface.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package gnverifier
22

33
import (
4+
"context"
5+
46
"github.com/gnames/gnlib/ent/gnvers"
57
vlib "github.com/gnames/gnlib/ent/verifier"
68
"github.com/gnames/gnquery/ent/search"
@@ -15,14 +17,14 @@ type GNverifier interface {
1517
VerifyOne(name string) (vlib.Name, error)
1618

1719
// VerifyBatch takes a slice of names and verifies them all at once
18-
VerifyBatch(names []string) []vlib.Name
20+
VerifyBatch(ctx context.Context, names []string) []vlib.Name
1921

2022
// VerifyStream receves batches of strings via one channel, verifies
2123
// the strings and sends results to another channel.
22-
VerifyStream(in <-chan []string, out chan []vlib.Name)
24+
VerifyStream(ctx context.Context, in <-chan []string, out chan []vlib.Name)
2325

2426
// Search provides faceted search functionality.
25-
Search(search.Input) ([]vlib.Name, error)
27+
Search(ctx context.Context, srch search.Input) ([]vlib.Name, error)
2628

2729
// ChangeConfig modifies configuration of GNverifier.
2830
ChangeConfig(opts ...config.Option) GNverifier

io/web/server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package web
22

33
import (
4+
"context"
45
"embed"
56
"fmt"
67
"net/http"
@@ -256,7 +257,7 @@ func verificationResults(
256257
if all := gnv.Config().WithAllMatches; all {
257258
inp.WithAllMatches = all
258259
}
259-
data.Verified, err = gnv.Search(inp)
260+
data.Verified, err = gnv.Search(context.Background(), inp)
260261
if err != nil {
261262
log.Warn(err)
262263
}
@@ -274,7 +275,7 @@ func verificationResults(
274275
}
275276
}
276277
} else {
277-
data.Verified = gnv.VerifyBatch(names)
278+
data.Verified = gnv.VerifyBatch(context.Background(), names)
278279

279280
if l := len(names); l > 0 {
280281
zlog.Info().

version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package gnverifier
22

33
var (
44
// Version of the gnverifier
5-
Version = "v0.8.2+"
5+
Version = "v0.9.0+"
66
// Build timestamp
77
Build = "n/a"
88
)

0 commit comments

Comments
 (0)