mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-24 12:32:21 +00:00
This commit is contained in:
parent
105da782d9
commit
caa0429d06
@ -96,8 +96,8 @@ func NewKVStoreService() *KVStoreService {
|
|||||||
|
|
||||||
```go
|
```go
|
||||||
func (p *KVStoreService) Get(key string, value *string) error {
|
func (p *KVStoreService) Get(key string, value *string) error {
|
||||||
p.Lock()
|
p.mu.Lock()
|
||||||
defer p.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
if v, ok := p.m[key]; ok {
|
if v, ok := p.m[key]; ok {
|
||||||
*value = v
|
*value = v
|
||||||
@ -108,8 +108,8 @@ func (p *KVStoreService) Get(key string, value *string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *KVStoreService) Set(kv [2]string, reply *struct{}) error {
|
func (p *KVStoreService) Set(kv [2]string, reply *struct{}) error {
|
||||||
p.Lock()
|
p.mu.Lock()
|
||||||
defer p.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
key, value := kv[0], kv[1]
|
key, value := kv[0], kv[1]
|
||||||
|
|
||||||
@ -133,9 +133,9 @@ func (p *KVStoreService) Watch(timeoutSecond int, keyChanged *string) error {
|
|||||||
id := fmt.Sprintf("watch-%s-%03d", time.Now(), rand.Int())
|
id := fmt.Sprintf("watch-%s-%03d", time.Now(), rand.Int())
|
||||||
ch := make(chan string, 10) // buffered
|
ch := make(chan string, 10) // buffered
|
||||||
|
|
||||||
p.Lock()
|
p.mu.Lock()
|
||||||
p.filter[id] = func(key string) { ch <- key }
|
p.filter[id] = func(key string) { ch <- key }
|
||||||
p.Unlock()
|
p.mu.Unlock()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(time.Duration(timeoutSecond) * time.Second):
|
case <-time.After(time.Duration(timeoutSecond) * time.Second):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user