forked from vaskoz/coffee-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
112 lines (88 loc) · 2.03 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"errors"
"log"
"sync"
"time"
)
// INTERFACES
//
// Store represents a place where customers, baristas and orders are filled
type Store interface {
Open(context.Context) (<-chan struct{}, error)
CloseAfter(time time.Duration)
Customers([]Customer)
Baristas([]Barista)
//isUnique()
}
// HIDDEN STRUCTS THAT IMPLEMENT INTERFACES
//
type store struct {
baristas []Barista
customers []Customer
openTime time.Duration
baristaQueue chan Barista
customerQueue chan Customer
logger *log.Logger
}
// HIDDEN DEFAULT METHOD IMPLEMENTATIONS
//
// METHODS
//
//func (s *store) isUnique() {}
func (s *store) Open(ctx context.Context) (<-chan struct{}, error) {
if s.openTime == 0 || len(s.customers) == 0 || len(s.baristas) == 0 {
return nil, errors.New("can't open store. not configured properly")
}
ctx, cancel := context.WithTimeout(ctx, s.openTime)
done := make(chan struct{}, 1)
var wg sync.WaitGroup
go func() {
for {
select {
case <-ctx.Done():
s.logger.Println("Store is closing")
wg.Wait()
cancel()
done <- struct{}{}
return
case c := <-s.customerQueue:
b := <-s.baristaQueue
wg.Add(1)
go func() {
o := b.MakeOrder(c.PlaceOrder())
s.baristaQueue <- b
s.logger.Println(c.EnjoyBeverage(o, b))
s.customerQueue <- c
wg.Done()
}()
}
}
}()
return done, nil
}
func (s *store) CloseAfter(time time.Duration) {
s.openTime = time
}
func (s *store) Customers(customers []Customer) {
s.customers = customers
s.customerQueue = make(chan Customer, len(customers)+1)
for _, c := range s.customers {
s.customerQueue <- c
}
}
func (s *store) Baristas(baristas []Barista) {
s.baristas = baristas
s.baristaQueue = make(chan Barista, len(baristas)+1)
for _, b := range s.baristas {
s.baristaQueue <- b
}
}
// CONSTRUCTOR/FACTORY/BUILDER
// OTHER PACKAGE LEVEL EXPORTED FUNCTIONS
// NewStore creates a new store
// Pass it a logger
func NewStore(logger *log.Logger) Store {
return &store{logger: logger}
}