github.com/dubbogo/gost@v1.14.0/sync/options.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxsync
    19  
    20  import (
    21  	"fmt"
    22  )
    23  
    24  const (
    25  	defaultTaskQNumber = 10
    26  	defaultTaskQLen    = 128
    27  )
    28  
    29  /////////////////////////////////////////
    30  // Task Pool Options
    31  /////////////////////////////////////////
    32  
    33  // TaskPoolOptions is optional settings for task pool
    34  type TaskPoolOptions struct {
    35  	tQLen      int // task queue length. buffer size per queue
    36  	tQNumber   int // task queue number. number of queue
    37  	tQPoolSize int // task pool size. number of workers
    38  }
    39  
    40  func (o *TaskPoolOptions) validate() {
    41  	if o.tQPoolSize < 1 {
    42  		panic(fmt.Sprintf("illegal pool size %d", o.tQPoolSize))
    43  	}
    44  
    45  	if o.tQLen < 1 {
    46  		o.tQLen = defaultTaskQLen
    47  	}
    48  
    49  	if o.tQNumber < 1 {
    50  		o.tQNumber = defaultTaskQNumber
    51  	}
    52  
    53  	if o.tQNumber > o.tQPoolSize {
    54  		o.tQNumber = o.tQPoolSize
    55  	}
    56  }
    57  
    58  type TaskPoolOption func(*TaskPoolOptions)
    59  
    60  // WithTaskPoolTaskPoolSize set @size of the task queue pool size
    61  func WithTaskPoolTaskPoolSize(size int) TaskPoolOption {
    62  	return func(o *TaskPoolOptions) {
    63  		o.tQPoolSize = size
    64  	}
    65  }
    66  
    67  // WithTaskPoolTaskQueueLength set @length of the task queue length
    68  func WithTaskPoolTaskQueueLength(length int) TaskPoolOption {
    69  	return func(o *TaskPoolOptions) {
    70  		o.tQLen = length
    71  	}
    72  }
    73  
    74  // WithTaskPoolTaskQueueNumber set @number of the task queue number
    75  func WithTaskPoolTaskQueueNumber(number int) TaskPoolOption {
    76  	return func(o *TaskPoolOptions) {
    77  		o.tQNumber = number
    78  	}
    79  }