-
Notifications
You must be signed in to change notification settings - Fork 82
/
loopdriver.go
83 lines (74 loc) · 2.75 KB
/
loopdriver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2019 PayPal Inc.
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lib
import (
"database/sql"
"database/sql/driver"
"fmt"
"net"
"strings"
"github.com/paypal/hera/client/gosqldriver"
"github.com/paypal/hera/common"
"github.com/paypal/hera/utility/encoding/netstring"
"github.com/paypal/hera/utility/logger"
)
/*
* database/sql driver to be used internally by hera. That way components reading the configration from the database
* (like sharding configuration for example) can be coded in standard GO SQL.
* It is simply a wrapper over client/gosqldriver
*/
type heraLoopDriver struct {
}
// ConnHandlerFunc defines the signature of a fucntion that can be used as a callback by the loop driver
type ConnHandlerFunc func(net.Conn)
var connHandler ConnHandlerFunc
// RegisterLoopDriver installs the callback for the loop driver
func RegisterLoopDriver(f ConnHandlerFunc) {
connHandler = f
drvLoop := &heraLoopDriver{}
sql.Register("heraloop", drvLoop)
}
/**
URL: <ShardID>:<PoolType>:<PoolID>
TODO: add another parameter for debugging/troubleshooting, IDing the client
*/
func (driver *heraLoopDriver) Open(url string) (driver.Conn, error) {
cli, srv := net.Pipe()
go connHandler(srv)
if logger.GetLogger().V(logger.Debug) {
logger.GetLogger().Log(logger.Debug, "Hera loop driver driver, opening", url, ": ", cli)
}
if len(url) > 0 {
// now set the shard ID
fields := strings.Split(url, ":")
if (len(fields) == 3) && (GetConfig().EnableSharding) {
ns := netstring.NewNetstringFrom(common.CmdSetShardID, []byte(fields[0]))
cli.Write(ns.Serialized)
ns, err := netstring.NewNetstring(cli)
if err != nil {
return nil, fmt.Errorf("Failed to set shardID: %s", err.Error())
}
if ns.Cmd != common.RcOK {
return nil, fmt.Errorf("HERA_SET_SHARD_ID response: %s", string(ns.Serialized))
}
if logger.GetLogger().V(logger.Debug) {
logger.GetLogger().Log(logger.Debug, "HERA loop driver driver, opened to shard", fields[0])
}
}
}
return gosqldriver.NewHeraConnection(cli), nil
}