github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/logtail/service/client.go (about) 1 // Copyright 2021 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 service 16 17 import ( 18 "context" 19 20 "go.uber.org/ratelimit" 21 22 "github.com/matrixorigin/matrixone/pkg/common/moerr" 23 "github.com/matrixorigin/matrixone/pkg/common/morpc" 24 "github.com/matrixorigin/matrixone/pkg/pb/api" 25 "github.com/matrixorigin/matrixone/pkg/pb/logtail" 26 ) 27 28 type ClientOption func(*LogtailClient) 29 30 func WithClientRequestPerSecond(rps int) ClientOption { 31 return func(c *LogtailClient) { 32 c.options.rps = rps 33 } 34 } 35 36 // LogtailClient encapsulates morpc stream. 37 type LogtailClient struct { 38 stream morpc.Stream 39 recvChan chan morpc.Message 40 41 options struct { 42 rps int 43 } 44 45 limiter ratelimit.Limiter 46 } 47 48 // NewLogtailClient constructs LogtailClient. 49 func NewLogtailClient(stream morpc.Stream, opts ...ClientOption) (*LogtailClient, error) { 50 client := &LogtailClient{ 51 stream: stream, 52 } 53 54 recvChan, err := stream.Receive() 55 if err != nil { 56 return nil, err 57 } 58 client.recvChan = recvChan 59 60 client.options.rps = 200 61 for _, opt := range opts { 62 opt(client) 63 } 64 client.limiter = ratelimit.New(client.options.rps) 65 66 return client, nil 67 } 68 69 // Close closes stream. 70 func (c *LogtailClient) Close() error { 71 return c.stream.Close() 72 } 73 74 // Subscribe subscribes table. 75 func (c *LogtailClient) Subscribe( 76 ctx context.Context, table api.TableID, 77 ) error { 78 c.limiter.Take() 79 80 request := &LogtailRequest{} 81 request.Request = &logtail.LogtailRequest_SubscribeTable{ 82 SubscribeTable: &logtail.SubscribeRequest{ 83 Table: &table, 84 }, 85 } 86 request.SetID(c.stream.ID()) 87 return c.stream.Send(ctx, request) 88 } 89 90 // Unsubscribe cancel subscription for table. 91 func (c *LogtailClient) Unsubscribe( 92 ctx context.Context, table api.TableID, 93 ) error { 94 c.limiter.Take() 95 96 request := &LogtailRequest{} 97 request.Request = &logtail.LogtailRequest_UnsubscribeTable{ 98 UnsubscribeTable: &logtail.UnsubscribeRequest{ 99 Table: &table, 100 }, 101 } 102 request.SetID(c.stream.ID()) 103 return c.stream.Send(ctx, request) 104 } 105 106 // Receive fetches logtail response. 107 // 108 // 1. response for error: *LogtailResponse.GetError() != nil 109 // 2. response for subscription: *LogtailResponse.GetSubscribeResponse() != nil 110 // 3. response for unsubscription: *LogtailResponse.GetUnsubscribeResponse() != nil 111 // 3. response for additional logtail: *LogtailResponse.GetUpdateResponse() != nil 112 func (c *LogtailClient) Receive() (*LogtailResponse, error) { 113 recvFunc := func() (*LogtailResponseSegment, error) { 114 message, ok := <-c.recvChan 115 if !ok || message == nil { 116 return nil, moerr.NewStreamClosedNoCtx() 117 } 118 return message.(*LogtailResponseSegment), nil 119 } 120 121 prev, err := recvFunc() 122 if err != nil { 123 return nil, err 124 } 125 buf := make([]byte, 0, prev.MessageSize) 126 buf = AppendChunk(buf, prev.GetPayload()) 127 128 for prev.Sequence < prev.MaxSequence { 129 segment, err := recvFunc() 130 if err != nil { 131 return nil, err 132 } 133 buf = AppendChunk(buf, segment.GetPayload()) 134 prev = segment 135 } 136 137 resp := &LogtailResponse{} 138 if err := resp.Unmarshal(buf); err != nil { 139 return nil, err 140 } 141 return resp, nil 142 }