github.com/mailru/activerecord@v1.12.2/pkg/iproto/util/pool/poolflag/poolflag.go (about)

     1  package poolflag
     2  
     3  import (
     4  	"bytes"
     5  	"flag"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  /*
    12  // Get returns pool config factory function.
    13  //
    14  // It registers flag flags with given prefix that are necessary
    15  // for config construction.
    16  //
    17  // Call of returned function returns new instance of pool config.
    18  //
    19  // For example:
    20  //	var myPoolConfig = poolflag.Get("my");
    21  //
    22  //	func main() {
    23  //		config := myPoolConfig()
    24  //		p := pool.New(config)
    25  //	}
    26  //
    27  func Get(prefix string) func() *pool.Config {
    28  	return Export(flag.CommandLine, prefix)
    29  }
    30  
    31  // GetWithStat returns pool config factory function.
    32  //
    33  // It registers flag flags with given prefix that are necessary
    34  // for config construction. It also registers flags that helps to
    35  // configure statistics measuring.
    36  //
    37  // Call of this function returns new instance of pool config.
    38  //
    39  // Returned config's callback options are filled with stat functions.
    40  //
    41  // Currently, these statistics are measured:
    42  // 	- time of task queued (run_queue_time);
    43  //	- time of task execution (exec_time);
    44  //	- time of workers being idle (workers_idle_time);
    45  //	- count of queued tasks (queued_tasks);
    46  //	- throughput of incoming tasks (task_in);
    47  //	- throughput of tasks performed (task_out);
    48  //	- count of alive workers;
    49  func GetWithStat(prefix string) func(...stat.Tag) *pool.Config {
    50  	return ExportWithStat(flag.CommandLine, prefix)
    51  }
    52  
    53  // Export is the same as Get but uses given flag.FlagSet instead of
    54  // flag.CommandLine.
    55  func Export(flag *flag.FlagSet, prefix string) func() *pool.Config {
    56  	return config.Export(flag, prefix)
    57  }
    58  
    59  // ExportWithStat is the same as GetWithStat but uses given flag.FlagSet instead
    60  // of flag.CommandLine.
    61  func ExportWithStat(flag *flag.FlagSet, prefix string) func(...stat.Tag) *pool.Config {
    62  	return config.ExportWithStat(
    63  		flagSetWrapper{flag},
    64  		prefix,
    65  	)
    66  }
    67  */
    68  
    69  type flagSetWrapper struct {
    70  	*flag.FlagSet
    71  }
    72  
    73  func (f flagSetWrapper) Float64Slice(name string, def []float64, desc string) *[]float64 {
    74  	v := new(float64slice)
    75  	*v = float64slice(def)
    76  	f.Var(v, name, desc)
    77  
    78  	return (*[]float64)(v)
    79  }
    80  
    81  type float64slice []float64
    82  
    83  func (f *float64slice) Set(v string) (err error) {
    84  	var (
    85  		values = strings.Split(v, ",")
    86  		vs     = make([]float64, len(values))
    87  	)
    88  
    89  	for i, v := range values {
    90  		vs[i], err = strconv.ParseFloat(strings.TrimSpace(v), 64)
    91  		if err != nil {
    92  			return err
    93  		}
    94  	}
    95  
    96  	*f = float64slice(vs)
    97  
    98  	return nil
    99  }
   100  
   101  func (f *float64slice) String() string {
   102  	var buf bytes.Buffer
   103  
   104  	for i, f := range *f {
   105  		if i != 0 {
   106  			buf.WriteString(", ")
   107  		}
   108  
   109  		fmt.Fprintf(&buf, "%f", f)
   110  	}
   111  
   112  	return buf.String()
   113  }