-
Notifications
You must be signed in to change notification settings - Fork 9
/
client.go
56 lines (47 loc) · 1.37 KB
/
client.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
// Copyright 2021 Peanutzhen. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package peanutcache
import (
"context"
"fmt"
pb "github.com/peanutzhen/peanutcache/peanutcachepb"
"github.com/peanutzhen/peanutcache/registry"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
)
// client 模块实现peanutcache访问其他远程节点 从而获取缓存的能力
type client struct {
name string // 服务名称 pcache/ip:addr
}
// Fetch 从remote peer获取对应缓存值
func (c *client) Fetch(group string, key string) ([]byte, error) {
// 创建一个etcd client
cli, err := clientv3.New(defaultEtcdConfig)
if err != nil {
return nil, err
}
defer cli.Close()
// 发现服务 取得与服务的连接
conn, err := registry.EtcdDial(cli, c.name)
if err != nil {
return nil, err
}
defer conn.Close()
grpcClient := pb.NewPeanutCacheClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := grpcClient.Get(ctx, &pb.GetRequest{
Group: group,
Key: key,
})
if err != nil {
return nil, fmt.Errorf("could not get %s/%s from peer %s", group, key, c.name)
}
return resp.GetValue(), nil
}
func NewClient(service string) *client {
return &client{name: service}
}
// 测试Client是否实现了Fetcher接口
var _ Fetcher = (*client)(nil)