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