Merge pull request #8 from Leryan/doc

doc: thread safety prevention
This commit is contained in:
Alexis Viscogliosi 2019-10-27 17:07:12 +01:00 committed by GitHub
commit 43d274259e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 6 deletions

View file

@ -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")

2
go.mod
View file

@ -1,3 +1,3 @@
module github.com/expectedsh/go-sonic
go 1.12
go 1.13

View file

@ -62,5 +62,44 @@ 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)
event := []string{"some_text", "some_id"}
tryCrash := func() {
for {
// replace "event" with whatever is giving you events: pubsub, amqp messages…
events <- event
}
}
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
Hardware detail: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz