github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/redis/metadata.go (about)

     1  /*
     2  Copyright 2021 The Dapr Authors
     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      http://www.apache.org/licenses/LICENSE-2.0
     7  Unless required by applicable law or agreed to in writing, software
     8  distributed under the License is distributed on an "AS IS" BASIS,
     9  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  See the License for the specific language governing permissions and
    11  limitations under the License.
    12  */
    13  
    14  package redis
    15  
    16  import (
    17  	"fmt"
    18  	"strconv"
    19  	"time"
    20  )
    21  
    22  const (
    23  	maxRetries             = "maxRetries"
    24  	maxRetryBackoff        = "maxRetryBackoff"
    25  	ttlInSeconds           = "ttlInSeconds"
    26  	queryIndexes           = "queryIndexes"
    27  	defaultBase            = 10
    28  	defaultBitSize         = 0
    29  	defaultMaxRetries      = 3
    30  	defaultMaxRetryBackoff = time.Second * 2
    31  )
    32  
    33  type Metadata struct {
    34  	MaxRetries      int
    35  	MaxRetryBackoff time.Duration
    36  	TTLInSeconds    *int
    37  	QueryIndexes    string
    38  }
    39  
    40  func ParseRedisMetadata(properties map[string]string) (Metadata, error) {
    41  	m := Metadata{}
    42  
    43  	m.MaxRetries = defaultMaxRetries
    44  	if val, ok := properties[maxRetries]; ok && val != "" {
    45  		parsedVal, err := strconv.ParseInt(val, defaultBase, defaultBitSize)
    46  		if err != nil {
    47  			return m, fmt.Errorf("redis store error: can't parse maxRetries field: %s", err)
    48  		}
    49  		m.MaxRetries = int(parsedVal)
    50  	}
    51  
    52  	m.MaxRetryBackoff = defaultMaxRetryBackoff
    53  	if val, ok := properties[maxRetryBackoff]; ok && val != "" {
    54  		parsedVal, err := strconv.ParseInt(val, defaultBase, defaultBitSize)
    55  		if err != nil {
    56  			return m, fmt.Errorf("redis store error: can't parse maxRetryBackoff field: %s", err)
    57  		}
    58  		m.MaxRetryBackoff = time.Duration(parsedVal)
    59  	}
    60  
    61  	if val, ok := properties[ttlInSeconds]; ok && val != "" {
    62  		parsedVal, err := strconv.ParseInt(val, defaultBase, defaultBitSize)
    63  		if err != nil {
    64  			return m, fmt.Errorf("redis store error: can't parse ttlInSeconds field: %s", err)
    65  		}
    66  		intVal := int(parsedVal)
    67  		m.TTLInSeconds = &intVal
    68  	} else {
    69  		m.TTLInSeconds = nil
    70  	}
    71  
    72  	if val, ok := properties[queryIndexes]; ok && val != "" {
    73  		m.QueryIndexes = val
    74  	}
    75  	return m, nil
    76  }