...

Source file src/github.com/ory/x/sqlcon/connector.go

Documentation: github.com/ory/x/sqlcon

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   */
    20  
    21  // Package sqlcon provides helpers for dealing with SQL connectivity.
    22  package sqlcon
    23  
    24  import (
    25  	"fmt"
    26  	"net/url"
    27  	"runtime"
    28  	"strings"
    29  
    30  	"github.com/pkg/errors"
    31  )
    32  
    33  func cleanURLQuery(in url.Values) (out url.Values) {
    34  	out, _ = url.ParseQuery(in.Encode())
    35  	out.Del("max_conns")
    36  	out.Del("max_idle_conns")
    37  	out.Del("max_conn_lifetime")
    38  	out.Del("parseTime")
    39  	return out
    40  }
    41  
    42  // GetDriverName returns the driver name of a given DSN.
    43  func GetDriverName(dsn string) string {
    44  	return strings.Split(dsn, "://")[0]
    45  }
    46  
    47  func classifyDSN(dsn string) string {
    48  	scheme := strings.Split(dsn, "://")[0]
    49  	parts := strings.Split(dsn, "@")
    50  	host := parts[len(parts)-1]
    51  	return fmt.Sprintf("%s://*:*@%s", scheme, host)
    52  }
    53  
    54  func maxParallelism() int {
    55  	maxProcs := runtime.GOMAXPROCS(0)
    56  	numCPU := runtime.NumCPU()
    57  	if maxProcs < numCPU {
    58  		return maxProcs
    59  	}
    60  	return numCPU
    61  }
    62  
    63  func parseQuery(dsn string) (clean string, query url.Values, err error) {
    64  	query = url.Values{}
    65  	parts := strings.Split(dsn, "?")
    66  	clean = parts[0]
    67  	if len(parts) == 2 {
    68  		if query, err = url.ParseQuery(parts[1]); err != nil {
    69  			return "", query, errors.WithStack(err)
    70  		}
    71  	}
    72  	return
    73  }
    74  

View as plain text