Skip to content

Commit

Permalink
Camelcase in example.
Browse files Browse the repository at this point in the history
  • Loading branch information
stefano-ottolenghi committed Dec 22, 2023
1 parent 4dfcf44 commit ab2356c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions go-manual/modules/ROOT/pages/concurrency.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
}
// Run a query and get results in a channel
records_c := queryToChannel(ctx, driver) // <1>
recordsC := queryToChannel(ctx, driver) // <1>
// Spawn some consumers that will process records
// They communicate back on the log channel
Expand All @@ -52,7 +52,7 @@ func main() {
wg := &sync.WaitGroup{} // <5>
for i := 1; i < 10; i++ { // i starts from 1 because 0th receiver would process too fast
wg.Add(1)
go consumer(wg, records_c, log, i) // <6>
go consumer(wg, recordsC, log, i) // <6>
}
// When all consumers are done, close log channel
go func() {
Expand All @@ -66,7 +66,7 @@ func main() {
}
func queryToChannel(ctx context.Context, driver neo4j.DriverWithContext) chan *neo4j.Record {
records_c := make(chan *neo4j.Record, 10) // <2>
recordsC := make(chan *neo4j.Record, 10) // <2>
session := driver.NewSession(ctx, neo4j.SessionConfig{DatabaseName: "neo4j"})
defer session.Close(ctx)
go session.ExecuteWrite(ctx,
Expand All @@ -83,12 +83,12 @@ func queryToChannel(ctx context.Context, driver neo4j.DriverWithContext) chan *n
// Stream results to channel as they come from the server
for result.Next(ctx) { // <3>
record := result.Record()
records_c <- record
recordsC <- record
}
close(records_c)
close(recordsC)
return nil, err
})
return records_c
return recordsC
}
func consumer(wg *sync.WaitGroup, records <-chan *neo4j.Record, log chan string, n int) {
Expand All @@ -102,15 +102,15 @@ func consumer(wg *sync.WaitGroup, records <-chan *neo4j.Record, log chan string,

<1> A Goroutine runs the query to the Neo4j server with a xref:transactions.adoc[managed transaction].
Notice that the driver session is created _inside_ the routine, as sessions are not thread-safe.
<2> The channel `records_c` is where the query result records get streamed to.
<2> The channel `recordsC` is where the query result records get streamed to.
The transaction function from `ExecuteWrite()` writes to it, and the various ``consumer``s read from it.
It is buffered so that the driver does not retrieve records faster than what the consumers can handle.
<3> Each result record coming from the server is sent over the `records_c` channel.
<3> Each result record coming from the server is sent over the `recordsC` channel.
The streaming continues so long as there are records to be processed, after which the channel gets closed and the routine exits.
<4> The channel `log` is where the consumers comunicate on.
<5> A `sync.WaitGroup` is needed to know when all consumers are done, and thus the `log` channel can be closed.
<6> A number of ``consumer``s get started in separate Goroutines.
Each consumer reads and processes records from the `records_c` channel.
Each consumer reads and processes records from the `recordsC` channel.
Each consumer simulates a lengthy operation with a sleeping timer.


Expand Down

0 comments on commit ab2356c

Please sign in to comment.