github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/message.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package logservice
    16  
    17  import (
    18  	"fmt"
    19  	"sync"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    22  	pb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    23  )
    24  
    25  // RPCRequest is request message type used in morpc
    26  type RPCRequest struct {
    27  	pb.Request
    28  	payload []byte
    29  	pool    *sync.Pool
    30  }
    31  
    32  var _ morpc.PayloadMessage = (*RPCRequest)(nil)
    33  
    34  func (r *RPCRequest) Size() int {
    35  	return r.Request.Size() + len(r.payload)
    36  }
    37  
    38  func (r *RPCRequest) Release() {
    39  	if r.pool != nil {
    40  		r.Request = pb.Request{}
    41  		r.payload = nil
    42  		r.pool.Put(r)
    43  	}
    44  }
    45  
    46  func (r *RPCRequest) SetID(id uint64) {
    47  	r.RequestID = id
    48  }
    49  
    50  func (r *RPCRequest) GetID() uint64 {
    51  	return r.RequestID
    52  }
    53  
    54  func (r *RPCRequest) DebugString() string {
    55  	return fmt.Sprintf("%s:%p", r.Request.Method.String(), r.payload)
    56  }
    57  
    58  func (r *RPCRequest) GetPayloadField() []byte {
    59  	return r.payload
    60  }
    61  
    62  func (r *RPCRequest) SetPayloadField(data []byte) {
    63  	r.payload = data
    64  }
    65  
    66  // RPCResponse is response message type used in morpc
    67  type RPCResponse struct {
    68  	pb.Response
    69  	payload []byte
    70  	pool    *sync.Pool
    71  }
    72  
    73  var _ morpc.PayloadMessage = (*RPCResponse)(nil)
    74  
    75  func (r *RPCResponse) Release() {
    76  	if r.pool != nil {
    77  		r.Response = pb.Response{}
    78  		r.payload = nil
    79  		r.pool.Put(r)
    80  	}
    81  }
    82  
    83  func (r *RPCResponse) SetID(id uint64) {
    84  	r.RequestID = id
    85  }
    86  
    87  func (r *RPCResponse) GetID() uint64 {
    88  	return r.RequestID
    89  }
    90  
    91  func (r *RPCResponse) DebugString() string {
    92  	return r.Response.Method.String()
    93  }
    94  
    95  func (r *RPCResponse) GetPayloadField() []byte {
    96  	return r.payload
    97  }
    98  
    99  func (r *RPCResponse) SetPayloadField(data []byte) {
   100  	r.payload = data
   101  }