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) {
req := &pb.GetRequest{
Group: &g.name,
Key: &key,
Group: g.name,
Key: key,
}
res := &pb.GetResponse{}
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
if res.Expire != nil && *res.Expire != 0 {
expire = time.Unix(*res.Expire/int64(time.Second), *res.Expire%int64(time.Second))
if res.Expire != 0 {
expire = time.Unix(res.Expire/int64(time.Second), res.Expire%int64(time.Second))
if time.Now().After(expire) {
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()
}
req := &pb.SetRequest{
Expire: &expire,
Group: &g.name,
Key: &k,
Value: v,
}
if hotCache {
req.HotCache = &hotCache
Expire: expire,
Group: g.name,
Key: k,
Value: v,
HotCache: hotCache,
}
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 {
req := &pb.GetRequest{
Group: &g.name,
Key: &key,
Group: g.name,
Key: key,
}
return peer.Remove(ctx, req)
}

View File

@ -40,8 +40,8 @@ type GetRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"`
Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"` // not actually required/guaranteed to be UTF-8
Group string `protobuf:"bytes,1,opt,name=group,proto3" json:"group,omitempty"`
Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` // not actually required/guaranteed to be UTF-8
}
func (x *GetRequest) Reset() {
@ -77,15 +77,15 @@ func (*GetRequest) Descriptor() ([]byte, []int) {
}
func (x *GetRequest) GetGroup() string {
if x != nil && x.Group != nil {
return *x.Group
if x != nil {
return x.Group
}
return ""
}
func (x *GetRequest) GetKey() string {
if x != nil && x.Key != nil {
return *x.Key
if x != nil {
return x.Key
}
return ""
}
@ -95,9 +95,9 @@ type GetResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Value []byte `protobuf:"bytes,1,opt,name=value" json:"value,omitempty"`
MinuteQps *float64 `protobuf:"fixed64,2,opt,name=minute_qps,json=minuteQps" json:"minute_qps,omitempty"`
Expire *int64 `protobuf:"varint,3,opt,name=expire" json:"expire,omitempty"`
Value []byte `protobuf:"bytes,1,opt,name=value,proto3" json:"value,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,proto3" json:"expire,omitempty"`
}
func (x *GetResponse) Reset() {
@ -140,15 +140,15 @@ func (x *GetResponse) GetValue() []byte {
}
func (x *GetResponse) GetMinuteQps() float64 {
if x != nil && x.MinuteQps != nil {
return *x.MinuteQps
if x != nil {
return x.MinuteQps
}
return 0
}
func (x *GetResponse) GetExpire() int64 {
if x != nil && x.Expire != nil {
return *x.Expire
if x != nil {
return x.Expire
}
return 0
}
@ -158,11 +158,11 @@ type SetRequest struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Group *string `protobuf:"bytes,1,req,name=group" json:"group,omitempty"`
Key *string `protobuf:"bytes,2,req,name=key" json:"key,omitempty"`
Value []byte `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"`
Expire *int64 `protobuf:"varint,4,opt,name=expire" json:"expire,omitempty"`
HotCache *bool `protobuf:"varint,5,opt,name=hotCache" json:"hotCache,omitempty"`
Group string `protobuf:"bytes,1,opt,name=group,proto3" json:"group,omitempty"`
Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"`
Value []byte `protobuf:"bytes,3,opt,name=value,proto3" json:"value,omitempty"`
Expire int64 `protobuf:"varint,4,opt,name=expire,proto3" json:"expire,omitempty"`
HotCache bool `protobuf:"varint,5,opt,name=hotCache,proto3" json:"hotCache,omitempty"`
}
func (x *SetRequest) Reset() {
@ -198,15 +198,15 @@ func (*SetRequest) Descriptor() ([]byte, []int) {
}
func (x *SetRequest) GetGroup() string {
if x != nil && x.Group != nil {
return *x.Group
if x != nil {
return x.Group
}
return ""
}
func (x *SetRequest) GetKey() string {
if x != nil && x.Key != nil {
return *x.Key
if x != nil {
return x.Key
}
return ""
}
@ -219,15 +219,15 @@ func (x *SetRequest) GetValue() []byte {
}
func (x *SetRequest) GetExpire() int64 {
if x != nil && x.Expire != nil {
return *x.Expire
if x != nil {
return x.Expire
}
return 0
}
func (x *SetRequest) GetHotCache() bool {
if x != nil && x.HotCache != nil {
return *x.HotCache
if x != nil {
return x.HotCache
}
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,
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,
0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x01, 0x20, 0x02, 0x28, 0x09, 0x52, 0x05, 0x67,
0x72, 0x6f, 0x75, 0x70, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x02, 0x28,
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, 0x01, 0x28,
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,
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,
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,
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,
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,
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,
@ -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,
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,
0x5a, 0x02, 0x2e, 0x2f,
0x5a, 0x02, 0x2e, 0x2f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (

View File

@ -14,31 +14,31 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
syntax = "proto2";
syntax = "proto3";
package groupcachepb;
option go_package = "./";
message GetRequest {
required string group = 1;
required string key = 2; // not actually required/guaranteed to be UTF-8
string group = 1;
string key = 2; // not actually required/guaranteed to be UTF-8
}
message GetResponse {
optional bytes value = 1;
optional double minute_qps = 2;
optional int64 expire = 3;
bytes value = 1;
double minute_qps = 2;
int64 expire = 3;
}
message SetRequest {
required string group = 1;
required string key = 2;
optional bytes value = 3;
optional int64 expire = 4;
optional bool hotCache = 5;
string group = 1;
string key = 2;
bytes value = 3;
int64 expire = 4;
bool hotCache = 5;
}
service GroupCache {
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
if out.Expire != nil && *out.Expire != 0 {
expire = time.Unix(*out.Expire/int64(time.Second), *out.Expire%int64(time.Second))
if out.Expire != 0 {
expire = time.Unix(out.Expire/int64(time.Second), out.Expire%int64(time.Second))
}
c := &group.mainCache
if out.HotCache != nil && *out.HotCache {
if out.HotCache {
c = &group.hotCache
}
group.localSet(*out.Key, out.Value, expire, c)
group.localSet(out.Key, out.Value, expire, c)
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.
body, err := proto.Marshal(&pb.GetResponse{Value: b, Expire: &expireNano})
body, err := proto.Marshal(&pb.GetResponse{Value: b, Expire: expireNano})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return