-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from lovoo/248-connection-state
248 connection state
- Loading branch information
Showing
11 changed files
with
402 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
## Reconnecting View | ||
|
||
This example shows a reconnecting view by observing the state changes. | ||
Run a local Kafka cluster by calling `make start` in folder `examples/`. | ||
|
||
Then run this example (`go run 6-reconnecting-views/main.go`). | ||
You should see the view state changes upon starting. | ||
|
||
Now kill the kafka cluster `make stop`, you should see some error messages and the view | ||
trying to reconnect using a default backoff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/lovoo/goka" | ||
"github.com/lovoo/goka/codec" | ||
) | ||
|
||
func main() { | ||
|
||
view, err := goka.NewView( | ||
// connect to example kafka cluster | ||
[]string{"localhost:9092"}, | ||
// name does not matter, table will be empty | ||
"restartable-view-test-table", | ||
// codec doesn't matter, the table will be empty | ||
new(codec.String), | ||
// start the view autoconnecting | ||
goka.WithViewAutoReconnect(), | ||
) | ||
if err != nil { | ||
log.Fatalf("Cannot create view: %v", err) | ||
} | ||
// context we'll use to run the view and the state change observer | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
// channel used to wait for the view to finish | ||
done := make(chan struct{}) | ||
go func() { | ||
defer close(done) | ||
err := view.Run(ctx) | ||
if err != nil { | ||
log.Printf("View finished with error: %v", err) | ||
} | ||
}() | ||
|
||
// Get a state change observer and | ||
go func() { | ||
obs := view.ObserveStateChanges() | ||
defer obs.Stop() | ||
for { | ||
select { | ||
case state, ok := <-obs.C(): | ||
if !ok { | ||
return | ||
} | ||
log.Printf("View is in state: %v", goka.ViewState(state)) | ||
case <-ctx.Done(): | ||
return | ||
} | ||
} | ||
}() | ||
|
||
go func() { | ||
waiter := make(chan os.Signal, 1) | ||
signal.Notify(waiter, syscall.SIGINT, syscall.SIGTERM) | ||
<-waiter | ||
cancel() | ||
}() | ||
|
||
<-done | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.