Switch to proto3

This commit is contained in:
William Bergeron-Drouin 2024-04-26 15:09:50 -04:00
parent 478b3fdb18
commit aabc1abc8c
No known key found for this signature in database
GPG Key ID: F893C57A8CDB529A
4 changed files with 59 additions and 62 deletions

View File

@ -506,8 +506,8 @@ func (g *Group) getLocally(ctx context.Context, key string, dest Sink) (ByteView
func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (ByteView, error) { func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (ByteView, error) {
req := &pb.GetRequest{ req := &pb.GetRequest{
Group: &g.name, Group: g.name,
Key: &key, Key: key,
} }
res := &pb.GetResponse{} res := &pb.GetResponse{}
err := peer.Get(ctx, req, res) err := peer.Get(ctx, req, res)
@ -516,8 +516,8 @@ func (g *Group) getFromPeer(ctx context.Context, peer ProtoGetter, key string) (
} }
var expire time.Time var expire time.Time
if res.Expire != nil && *res.Expire != 0 { if res.Expire != 0 {
expire = time.Unix(*res.Expire/int64(time.Second), *res.Expire%int64(time.Second)) expire = time.Unix(res.Expire/int64(time.Second), res.Expire%int64(time.Second))
if time.Now().After(expire) { if time.Now().After(expire) {
return ByteView{}, errors.New("peer returned expired value") return ByteView{}, errors.New("peer returned expired value")
} }
@ -536,14 +536,11 @@ func (g *Group) setFromPeer(ctx context.Context, peer ProtoGetter, k string, v [
expire = e.UnixNano() expire = e.UnixNano()
} }
req := &pb.SetRequest{ req := &pb.SetRequest{
Expire: &expire, Expire: expire,
Group: &g.name, Group: g.name,
Key: &k, Key: k,
Value: v, Value: v,
} HotCache: hotCache,
if hotCache {
req.HotCache = &hotCache
} }
return peer.Set(ctx, req) return peer.Set(ctx, req)
@ -551,8 +548,8 @@ func (g *Group) setFromPeer(ctx context.Context, peer ProtoGetter, k string, v [
func (g *Group) removeFromPeer(ctx context.Context, peer ProtoGetter, key string) error { func (g *Group) removeFromPeer(ctx context.Context, peer ProtoGetter, key string) error {
req := &pb.GetRequest{ req := &pb.GetRequest{
Group: &g.name, Group: g.name,
Key: &key, Key: key,
} }
return peer.Remove(ctx, req) return peer.Remove(ctx, req)
} }

View File

@ -40,8 +40,8 @@ type GetRequest struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"` Group string `protobuf:"bytes,1,opt,name=group,proto3" json:"group,omitempty"`
Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"` // not actually required/guaranteed to be UTF-8 Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` // not actually required/guaranteed to be UTF-8
} }
func (x *GetRequest) Reset() { func (x *GetRequest) Reset() {
@ -77,15 +77,15 @@ func (*GetRequest) Descriptor() ([]byte, []int) {
} }
func (x *GetRequest) GetGroup() string { func (x *GetRequest) GetGroup() string {
if x != nil && x.Group != nil { if x != nil {
return *x.Group return x.Group
} }
return "" return ""
} }
func (x *GetRequest) GetKey() string { func (x *GetRequest) GetKey() string {
if x != nil && x.Key != nil { if x != nil {
return *x.Key return x.Key
} }
return "" return ""
} }
@ -95,9 +95,9 @@ type GetResponse struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Value []byte `protobuf:"bytes,1,opt,name=value" json:"value,omitempty"` Value []byte `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
MinuteQps *float64 `protobuf:"fixed64,2,opt,name=minute_qps,json=minuteQps" json:"minute_qps,omitempty"` MinuteQps float64 `protobuf:"fixed64,2,opt,name=minute_qps,json=minuteQps,proto3" json:"minute_qps,omitempty"`
Expire *int64 `protobuf:"varint,3,opt,name=expire" json:"expire,omitempty"` Expire int64 `protobuf:"varint,3,opt,name=expire,proto3" json:"expire,omitempty"`
} }
func (x *GetResponse) Reset() { func (x *GetResponse) Reset() {
@ -140,15 +140,15 @@ func (x *GetResponse) GetValue() []byte {
} }
func (x *GetResponse) GetMinuteQps() float64 { func (x *GetResponse) GetMinuteQps() float64 {
if x != nil && x.MinuteQps != nil { if x != nil {
return *x.MinuteQps return x.MinuteQps
} }
return 0 return 0
} }
func (x *GetResponse) GetExpire() int64 { func (x *GetResponse) GetExpire() int64 {
if x != nil && x.Expire != nil { if x != nil {
return *x.Expire return x.Expire
} }
return 0 return 0
} }
@ -158,11 +158,11 @@ type SetRequest struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"` Group string `protobuf:"bytes,1,opt,name=group,proto3" json:"group,omitempty"`
Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"` Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
Value []byte `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"` Value []byte `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty"`
Expire *int64 `protobuf:"varint,4,opt,name=expire" json:"expire,omitempty"` Expire int64 `protobuf:"varint,4,opt,name=expire,proto3" json:"expire,omitempty"`
HotCache *bool `protobuf:"varint,5,opt,name=hotCache" json:"hotCache,omitempty"` HotCache bool `protobuf:"varint,5,opt,name=hotCache,proto3" json:"hotCache,omitempty"`
} }
func (x *SetRequest) Reset() { func (x *SetRequest) Reset() {
@ -198,15 +198,15 @@ func (*SetRequest) Descriptor() ([]byte, []int) {
} }
func (x *SetRequest) GetGroup() string { func (x *SetRequest) GetGroup() string {
if x != nil && x.Group != nil { if x != nil {
return *x.Group return x.Group
} }
return "" return ""
} }
func (x *SetRequest) GetKey() string { func (x *SetRequest) GetKey() string {
if x != nil && x.Key != nil { if x != nil {
return *x.Key return x.Key
} }
return "" return ""
} }
@ -219,15 +219,15 @@ func (x *SetRequest) GetValue() []byte {
} }
func (x *SetRequest) GetExpire() int64 { func (x *SetRequest) GetExpire() int64 {
if x != nil && x.Expire != nil { if x != nil {
return *x.Expire return x.Expire
} }
return 0 return 0
} }
func (x *SetRequest) GetHotCache() bool { func (x *SetRequest) GetHotCache() bool {
if x != nil && x.HotCache != nil { if x != nil {
return *x.HotCache return x.HotCache
} }
return false return false
} }
@ -238,8 +238,8 @@ var file_groupcache_proto_rawDesc = []byte{
0x0a, 0x10, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x0a, 0x10, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x12, 0x0c, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x63, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x74, 0x6f, 0x12, 0x0c, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x63, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62,
0x22, 0x34, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x22, 0x34, 0x0a, 0x0a, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14,
0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x01, 0x20, 0x02, 0x28, 0x09, 0x52, 0x05, 0x67, 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x67,
0x72, 0x6f, 0x75, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x02, 0x28, 0x72, 0x6f, 0x75, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28,
0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x5a, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x5a, 0x0a, 0x0b, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73,
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01,
0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1d, 0x0a, 0x0a, 0x6d,
@ -247,9 +247,9 @@ var file_groupcache_proto_rawDesc = []byte{
0x09, 0x6d, 0x69, 0x6e, 0x75, 0x74, 0x65, 0x51, 0x70, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x65, 0x78, 0x09, 0x6d, 0x69, 0x6e, 0x75, 0x74, 0x65, 0x51, 0x70, 0x73, 0x12, 0x16, 0x0a, 0x06, 0x65, 0x78,
0x70, 0x69, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x65, 0x78, 0x70, 0x69, 0x70, 0x69, 0x72, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x65, 0x78, 0x70, 0x69,
0x72, 0x65, 0x22, 0x7e, 0x0a, 0x0a, 0x53, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x72, 0x65, 0x22, 0x7e, 0x0a, 0x0a, 0x53, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
0x12, 0x14, 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x01, 0x20, 0x02, 0x28, 0x09, 0x52, 0x12, 0x14, 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20,
0x02, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75,
0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x16,
0x0a, 0x06, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x0a, 0x06, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06,
0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x68, 0x6f, 0x74, 0x43, 0x61, 0x63, 0x65, 0x78, 0x70, 0x69, 0x72, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x68, 0x6f, 0x74, 0x43, 0x61, 0x63,
@ -259,7 +259,7 @@ var file_groupcache_proto_rawDesc = []byte{
0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x1a, 0x19, 0x2e, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x63, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62, 0x74, 0x1a, 0x19, 0x2e, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x63, 0x61, 0x63, 0x68, 0x65, 0x70, 0x62,
0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x04, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x04,
0x5a, 0x02, 0x2e, 0x2f, 0x5a, 0x02, 0x2e, 0x2f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
} }
var ( var (

View File

@ -14,31 +14,31 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
syntax = "proto2"; syntax = "proto3";
package groupcachepb; package groupcachepb;
option go_package = "./"; option go_package = "./";
message GetRequest { message GetRequest {
required string group = 1; string group = 1;
required string key = 2; // not actually required/guaranteed to be UTF-8 string key = 2; // not actually required/guaranteed to be UTF-8
} }
message GetResponse { message GetResponse {
optional bytes value = 1; bytes value = 1;
optional double minute_qps = 2; double minute_qps = 2;
optional int64 expire = 3; int64 expire = 3;
} }
message SetRequest { message SetRequest {
required string group = 1; string group = 1;
required string key = 2; string key = 2;
optional bytes value = 3; bytes value = 3;
optional int64 expire = 4; int64 expire = 4;
optional bool hotCache = 5; bool hotCache = 5;
} }
service GroupCache { service GroupCache {
rpc Get(GetRequest) returns (GetResponse) { rpc Get(GetRequest) returns (GetResponse) {
}; }
} }

10
http.go
View File

@ -213,16 +213,16 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
var expire time.Time var expire time.Time
if out.Expire != nil && *out.Expire != 0 { if out.Expire != 0 {
expire = time.Unix(*out.Expire/int64(time.Second), *out.Expire%int64(time.Second)) expire = time.Unix(out.Expire/int64(time.Second), out.Expire%int64(time.Second))
} }
c := &group.mainCache c := &group.mainCache
if out.HotCache != nil && *out.HotCache { if out.HotCache {
c = &group.hotCache c = &group.hotCache
} }
group.localSet(*out.Key, out.Value, expire, c) group.localSet(out.Key, out.Value, expire, c)
return return
} }
@ -250,7 +250,7 @@ func (p *HTTPPool) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// Write the value to the response body as a proto message. // Write the value to the response body as a proto message.
body, err := proto.Marshal(&pb.GetResponse{Value: b, Expire: &expireNano}) body, err := proto.Marshal(&pb.GetResponse{Value: b, Expire: expireNano})
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return