github.com/matrixorigin/matrixone@v1.2.0/pkg/logservice/option.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 "context" 19 pb "github.com/matrixorigin/matrixone/pkg/pb/logservice" 20 "github.com/matrixorigin/matrixone/pkg/util" 21 22 "github.com/matrixorigin/matrixone/pkg/common/morpc" 23 "github.com/matrixorigin/matrixone/pkg/common/runtime" 24 "github.com/matrixorigin/matrixone/pkg/taskservice" 25 ) 26 27 // Option is utility that sets callback for Service. 28 type Option func(*Service) 29 30 // WithBackendFilter sets filter via which could select remote backend. 31 func WithBackendFilter(filter func(morpc.Message, string) bool) Option { 32 return func(s *Service) { 33 s.options.backendFilter = filter 34 } 35 } 36 37 // WithRuntime sets runtime 38 func WithRuntime(runtime runtime.Runtime) Option { 39 return func(s *Service) { 40 s.runtime = runtime 41 } 42 } 43 44 // WithTaskStorageFactory set up the special task storage factory 45 func WithTaskStorageFactory(factory taskservice.TaskStorageFactory) Option { 46 return func(s *Service) { 47 s.task.storageFactory = factory 48 } 49 } 50 51 // WithConfigData saves the data from the config file 52 func WithConfigData(data map[string]*pb.ConfigItem) Option { 53 return func(s *Service) { 54 if s.config == nil { 55 s.config = util.NewConfigData(data) 56 } else { 57 util.MergeConfig(s.config, data) 58 } 59 } 60 } 61 62 type ContextKey string 63 64 const ( 65 BackendOption ContextKey = "morpc.BackendOption" 66 ClientOption ContextKey = "morpc.ClientOption" 67 ) 68 69 func GetBackendOptions(ctx context.Context) []morpc.BackendOption { 70 if v := ctx.Value(BackendOption); v != nil { 71 return v.([]morpc.BackendOption) 72 } 73 return nil 74 } 75 76 func GetClientOptions(ctx context.Context) []morpc.ClientOption { 77 if v := ctx.Value(ClientOption); v != nil { 78 return v.([]morpc.ClientOption) 79 } 80 return nil 81 } 82 83 func SetBackendOptions(ctx context.Context, opts ...morpc.BackendOption) context.Context { 84 return context.WithValue(ctx, BackendOption, opts) 85 } 86 87 func SetClientOptions(ctx context.Context, opts ...morpc.ClientOption) context.Context { 88 return context.WithValue(ctx, ClientOption, opts) 89 }