MCPcopy Index your code
hub / github.com/DoNewsCode/core / Example_server

Function Example_server

leader/example_server_test.go:32–58  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30}
31
32func Example_server() {
33 if os.Getenv("ETCD_ADDR") == "" {
34 fmt.Println("set ETCD_ADDR to run this example")
35 return
36 }
37 c := core.Default(core.WithInline("log.level", "none"))
38 c.Provide(otetcd.Providers())
39 c.Provide(leader.Providers())
40 c.Invoke(func(dispatcher contract.Dispatcher) {
41 // This listener will be called twice. Once on becoming the leader and once on resigning the leader.
42 dispatcher.Subscribe(events.Listen(leader.OnStatusChanged, func(ctx context.Context, event interface{}) error {
43 fmt.Println(event.(leader.OnStatusChangedPayload).Status.IsLeader())
44 return nil
45 }))
46 })
47 c.Invoke(func(sts *leader.Status) {
48 c.AddModule(ServerModule{Sts: sts})
49 })
50 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
51 defer cancel()
52
53 c.Serve(ctx)
54
55 // Output:
56 // true
57 // false
58}

Callers

nothing calls this directly

Calls 11

DefaultFunction · 0.92
WithInlineFunction · 0.92
ProvidersFunction · 0.92
ProvidersFunction · 0.92
ListenFunction · 0.92
ProvideMethod · 0.80
InvokeMethod · 0.80
IsLeaderMethod · 0.80
ServeMethod · 0.80
SubscribeMethod · 0.65
AddModuleMethod · 0.45

Tested by

no test coverage detected