Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db96bd9

Browse files
add the use of context in the distributed database example
1 parent ed643d2 commit db96bd9

File tree

6 files changed

+54
-31
lines changed

6 files changed

+54
-31
lines changed

‎mutexes/distributed-db/app/app.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package app
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
@@ -37,26 +38,35 @@ func New() (*App, error) {
3738
}
3839
w := workers.NewGossip(svc)
3940
a := &App{
40-
Server: srv,
41-
Worker: w,
41+
Server: srv,
42+
Worker: w,
4243
}
4344

4445
return a, nil
4546
}
4647

4748
type App struct {
48-
Server *http.Server
49-
Worker workers.Gossip
49+
Server *http.Server
50+
Worker workers.Gossip
5051
}
5152

52-
func (a App) Start() error {
53-
go a.Worker.Start()
53+
func (a App) Start(ctx context.Context) error {
54+
go a.Worker.Start(ctx)
5455

5556
log.Println("server started on address", a.Server.Addr)
5657
err := a.Server.ListenAndServe()
57-
if err != nil {
58+
if err != nil &&err!=http.ErrServerClosed{
5859
return err
5960
}
6061

6162
return nil
6263
}
64+
65+
func (a App) Stop(ctx context.Context) error {
66+
log.Println("shutting down the http server")
67+
err := a.Server.Shutdown(ctx)
68+
if err != nil && err != context.Canceled {
69+
return fmt.Errorf("could not stop the http server: %w", err)
70+
}
71+
return nil
72+
}

‎mutexes/distributed-db/main.go‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
package main
22

33
import (
4-
"log"
5-
4+
"context"
65
"distributed-db/app"
6+
"log"
7+
"os"
8+
"os/signal"
9+
"syscall"
710
)
811

912
func main() {
13+
ctx, cancel := context.WithCancel(context.Background())
14+
//defer cancel()
15+
signals := make(chan os.Signal, 1)
16+
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
17+
1018
a, err := app.New()
1119
if err != nil {
1220
log.Fatalf("could not create the app: %v", err)
1321
}
1422

15-
err = a.Start()
16-
if err != nil {
17-
log.Fatalf("could not start the app: %v", err)
23+
go func() {
24+
err = a.Start(ctx)
25+
if err != nil {
26+
log.Fatalf("could not start the app: %v", err)
27+
}
28+
}()
29+
30+
select {
31+
case <-signals:
32+
cancel()
33+
err = a.Stop(ctx)
34+
if err != nil {
35+
log.Fatalf("could not stop the app: %v", err)
36+
}
1837
}
1938
}

‎mutexes/distributed-db/repositories/cache.go‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,7 @@ func (c *Cache) Set(key, value string) models.CacheItem {
3434
return item
3535
}
3636

37-
func (c *Cache) Get(key string) *models.CacheItem {
38-
c.mu.RLock()
39-
defer c.mu.RUnlock()
40-
41-
item, ok := c.data[key]
42-
if !ok {
43-
return nil
44-
}
45-
46-
return &item
47-
}
48-
49-
func (c *Cache) GetMany(keys []string) []models.CacheItem {
37+
func (c *Cache) Get(keys []string) []models.CacheItem {
5038
c.mu.RLock()
5139
defer c.mu.RUnlock()
5240

‎mutexes/distributed-db/services/cache.go‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
)
1010

1111
type CacheRepository interface {
12-
Get(key string) *models.CacheItem
13-
GetMany(keys []string) []models.CacheItem
12+
Get(keys []string) []models.CacheItem
1413
Set(key, value string) models.CacheItem
1514
}
1615

@@ -57,7 +56,7 @@ func (svc CacheSvc) Get(keys []string) []models.CacheItem {
5756
cacheItems := make([]models.CacheItem, 0)
5857
for node, sums := range nodeToSums {
5958
if node == svc.tokens.Nodes.CurrentNode {
60-
items := svc.cacheRepo.GetMany(sums)
59+
items := svc.cacheRepo.Get(sums)
6160
for _, item := range items {
6261
item.Node = node
6362
cacheItems = append(cacheItems, item)

‎mutexes/distributed-db/workers/gossip.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package workers
22

33
import (
4+
"context"
45
"log"
56
"time"
67
)
@@ -21,10 +22,16 @@ type Gossip struct {
2122
svc gossiper
2223
}
2324

24-
func (g *Gossip) Start() {
25+
func (g *Gossip) Start(ctx context.Context) {
2526
log.Println("worker started successfully")
27+
2628
for {
27-
g.svc.Gossip()
28-
time.Sleep(gossipPeriod)
29+
select {
30+
case <-ctx.Done():
31+
log.Println("stopping the gossip worker")
32+
return
33+
case <-time.NewTicker(gossipPeriod).C:
34+
g.svc.Gossip()
35+
}
2936
}
3037
}
810 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /