github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logtail/service/request.go (about)

     1  // Copyright 2021 Matrix Origin
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  //	http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package service
    15  
    16  import (
    17  	"sync"
    18  
    19  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    20  	"github.com/matrixorigin/matrixone/pkg/pb/logtail"
    21  )
    22  
    23  // LogtailRequest wraps logtail.LogtailRequest.
    24  type LogtailRequest struct {
    25  	logtail.LogtailRequest
    26  }
    27  
    28  var _ morpc.Message = (*LogtailRequest)(nil)
    29  
    30  func (r *LogtailRequest) SetID(id uint64) {
    31  	r.RequestId = id
    32  }
    33  
    34  func (r *LogtailRequest) GetID() uint64 {
    35  	return r.RequestId
    36  }
    37  
    38  func (r *LogtailRequest) DebugString() string {
    39  	return r.LogtailRequest.String()
    40  }
    41  
    42  func (r *LogtailRequest) Size() int {
    43  	return r.ProtoSize()
    44  }
    45  
    46  // RequestPool acquires or releases LogtailRequest.
    47  type RequestPool interface {
    48  	// Acquire fetches item from pool.
    49  	Acquire() morpc.Message
    50  
    51  	// Release puts item back to pool.
    52  	Release(*LogtailRequest)
    53  }
    54  
    55  type requestPool struct {
    56  	pool *sync.Pool
    57  }
    58  
    59  func NewRequestPool() RequestPool {
    60  	return &requestPool{
    61  		pool: &sync.Pool{
    62  			New: func() any {
    63  				return &LogtailRequest{}
    64  			},
    65  		},
    66  	}
    67  }
    68  
    69  func (p *requestPool) Acquire() morpc.Message {
    70  	return p.pool.Get().(*LogtailRequest)
    71  }
    72  
    73  func (p *requestPool) Release(req *LogtailRequest) {
    74  	req.Reset()
    75  	p.pool.Put(req)
    76  }