From adfd010a937fabb62783ccae293cc1e8da0c7447 Mon Sep 17 00:00:00 2001 From: Florent Peterschmitt Date: Sun, 27 Oct 2019 13:25:09 +0100 Subject: [PATCH 1/2] thread safety + go1.13 --- cmd/example/main.go | 8 ++++---- go.mod | 2 +- readme.md | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/cmd/example/main.go b/cmd/example/main.go index 5cf19f7..c1266a9 100644 --- a/cmd/example/main.go +++ b/cmd/example/main.go @@ -15,10 +15,10 @@ func main() { // I will ignore all errors for demonstration purposes _ = ingester.BulkPush("movies", "general", 3, []sonic.IngestBulkRecord{ - {"id:6ab56b4kk3", "Star wars"}, - {"id:5hg67f8dg5", "Spider man"}, - {"id:1m2n3b4vf6", "Batman"}, - {"id:68d96h5h9d0", "This is another movie"}, + {Object: "id:6ab56b4kk3", Text: "Star wars"}, + {Object: "id:5hg67f8dg5", Text: "Spider man"}, + {Object: "id:1m2n3b4vf6", Text: "Batman"}, + {Object: "id:68d96h5h9d0", Text: "This is another movie"}, }) search, err := sonic.NewSearch("localhost", 1491, "SecretPassword") diff --git a/go.mod b/go.mod index de7ee70..04def90 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/expectedsh/go-sonic -go 1.12 +go 1.13 diff --git a/readme.md b/readme.md index bb9d0a0..b673057 100644 --- a/readme.md +++ b/readme.md @@ -62,5 +62,43 @@ BenchmarkIngesterChannel_Push-8 1 1023322864 ns/op PASS ``` +### Thread Safety + +The driver itself isn't thread safe. You could use locks or channels to avoid crashes. + +```go +package main + +import ( + "fmt" + + "github.com/expectedsh/go-sonic/sonic" +) + +func main() { + events := make(chan []string, 1) + + // simulating a high incoming message load + tryCrash := func() { + for { + events <- []string{"some_text", "some_id"} + } + } + + go tryCrash() + go tryCrash() + go tryCrash() + go tryCrash() + + ingester, _ := sonic.NewIngester("localhost", 1491, "SecretPassword") + + for { + msg := <-events + // Or use some buffering along with BulkPush + ingester.Push("collection", "bucket", msg[1], msg[0]) + } +} +``` + Bulk push is faster than for loop on Push. -Hardware detail: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz \ No newline at end of file +Hardware detail: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz From 2515bd3339ad76edb23a85c39a7ea8543b1cf6c3 Mon Sep 17 00:00:00 2001 From: Florent Peterschmitt Date: Sun, 27 Oct 2019 13:29:22 +0100 Subject: [PATCH 2/2] show what to replace --- readme.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index b673057..d8fe917 100644 --- a/readme.md +++ b/readme.md @@ -78,10 +78,11 @@ import ( func main() { events := make(chan []string, 1) - // simulating a high incoming message load + event := []string{"some_text", "some_id"} tryCrash := func() { for { - events <- []string{"some_text", "some_id"} + // replace "event" with whatever is giving you events: pubsub, amqp messages… + events <- event } }