...

Source file src/github.com/sigstore/rekor/pkg/indexstorage/mysql/options.go

Documentation: github.com/sigstore/rekor/pkg/indexstorage/mysql

     1  // Copyright 2023 The Sigstore Authors.
     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 mysql
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/jmoiron/sqlx"
    21  )
    22  
    23  // Options configures connections to the MySQL index storage system
    24  type Options interface {
    25  	applyConnMaxIdleTime(*sqlx.DB)
    26  	applyConnMaxLifetime(*sqlx.DB)
    27  	applyMaxIdleConns(*sqlx.DB)
    28  	applyMaxOpenConns(*sqlx.DB)
    29  }
    30  
    31  // NoOpOptionImpl implements the MySQLOption interfaces as no-ops.
    32  type noOpOptionImpl struct{}
    33  
    34  // applyConnMaxIdleTime is a no-op required to fully implement the requisite interfaces
    35  func (noOpOptionImpl) applyConnMaxIdleTime(_ *sqlx.DB) {}
    36  
    37  // ApplyConnMaxLifetime is a no-op required to fully implement the requisite interfaces
    38  func (noOpOptionImpl) applyConnMaxLifetime(_ *sqlx.DB) {}
    39  
    40  // ApplyMaxOpenConns is a no-op required to fully implement the requisite interfaces
    41  func (noOpOptionImpl) applyMaxOpenConns(_ *sqlx.DB) {}
    42  
    43  // ApplyMaxIdleConns is a no-op required to fully implement the requisite interfaces
    44  func (noOpOptionImpl) applyMaxIdleConns(_ *sqlx.DB) {}
    45  
    46  // RequestConnMaxIdleTime implements the functional option pattern for specifying the maximum connection idle time
    47  type RequestConnMaxIdleTime struct {
    48  	noOpOptionImpl
    49  	idleTime time.Duration
    50  }
    51  
    52  // applyConnMaxIdleTime sets the maximum connection idle time
    53  func (r RequestConnMaxIdleTime) applyConnMaxIdleTime(db *sqlx.DB) {
    54  	if db != nil {
    55  		db.SetConnMaxIdleTime(r.idleTime)
    56  	}
    57  }
    58  
    59  // WithConnMaxIdleTime specifies the maximum connection idle time
    60  func WithConnMaxIdleTime(idleTime time.Duration) RequestConnMaxIdleTime {
    61  	return RequestConnMaxIdleTime{idleTime: idleTime}
    62  }
    63  
    64  // RequestConnMaxLifetime implements the functional option pattern for specifying the maximum connection lifetime
    65  type RequestConnMaxLifetime struct {
    66  	noOpOptionImpl
    67  	lifetime time.Duration
    68  }
    69  
    70  // ApplyConnMaxLifetime sets the maximum connection lifetime
    71  func (r RequestConnMaxLifetime) applyConnMaxLifetime(db *sqlx.DB) {
    72  	if db != nil {
    73  		db.SetConnMaxLifetime(r.lifetime)
    74  	}
    75  }
    76  
    77  // WithConnMaxLifetime specifies the maximum connection lifetime
    78  func WithConnMaxLifetime(lifetime time.Duration) RequestConnMaxLifetime {
    79  	return RequestConnMaxLifetime{lifetime: lifetime}
    80  }
    81  
    82  // RequestMaxIdleConns implements the functional option pattern for specifying the maximum number of idle connections
    83  type RequestMaxIdleConns struct {
    84  	noOpOptionImpl
    85  	idleConns int
    86  }
    87  
    88  // ApplyMaxIdleConns sets the maximum number of idle connections
    89  func (r RequestMaxIdleConns) applyMaxIdleConns(db *sqlx.DB) {
    90  	if db != nil {
    91  		db.SetMaxIdleConns(r.idleConns)
    92  	}
    93  }
    94  
    95  // WithMaxIdleConns specifies the maximum number of idle connections
    96  func WithMaxIdleConns(idleConns int) RequestMaxIdleConns {
    97  	return RequestMaxIdleConns{idleConns: idleConns}
    98  }
    99  
   100  // RequestMaxOpenConns implements the functional option pattern for specifying the maximum number of open connections
   101  type RequestMaxOpenConns struct {
   102  	noOpOptionImpl
   103  	openConns int
   104  }
   105  
   106  // applyMaxOpenConns sets the maximum number of open connections
   107  func (r RequestMaxOpenConns) applyMaxOpenConns(db *sqlx.DB) {
   108  	if db != nil {
   109  		db.SetMaxOpenConns(r.openConns)
   110  	}
   111  }
   112  
   113  // WithMaxOpenConns specifies the maximum number of open connections
   114  func WithMaxOpenConns(openConns int) RequestMaxOpenConns {
   115  	return RequestMaxOpenConns{openConns: openConns}
   116  }
   117  

View as plain text