-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
112 lines (94 loc) · 2.77 KB
/
conn.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2020 Satoshi Konno. All rights reserved.
//
// Licensed 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 mysql
import (
"sync"
"time"
"github.com/cybergarage/go-tracing/tracer"
"github.com/google/uuid"
vitessmy "vitess.io/vitess/go/mysql"
)
// Conn represents a connection of MySQL binary protocol.
type Conn struct {
*vitessmy.Conn
db string
uid uint32
uuid uuid.UUID
ts time.Time
sync.Map
tracer.Context
}
// newConn returns a connection with a default empty connection.
func newConn() *Conn {
return NewConnWith(
tracer.NewNullTracer().StartSpan(""),
&vitessmy.Conn{}, // nolint: exhaustruct
)
}
// NewConnWith returns a connection with a raw connection.
func NewConnWith(ctx tracer.Context, c *vitessmy.Conn) *Conn {
conn := &Conn{
Conn: c,
db: "",
uid: 0,
uuid: uuid.New(),
ts: time.Now(),
Map: sync.Map{},
Context: ctx,
}
if c != nil {
conn.uid = c.ConnectionID
}
return conn
}
// SetDeadline sets the read and write deadlines associated with the connection.
func (conn *Conn) SetDeadline(t time.Time) error {
return conn.GetRawConn().SetDeadline(t)
}
// SetReadDeadline sets the deadline for future Read calls.
func (conn *Conn) SetReadDeadline(t time.Time) error {
return conn.GetRawConn().SetReadDeadline(t)
}
// SetWriteDeadline sets the deadline for future Write calls.
func (conn *Conn) SetWriteDeadline(t time.Time) error {
return conn.GetRawConn().SetWriteDeadline(t)
}
// SetDatabase sets th selected database to the connection.
func (conn *Conn) SetDatabase(name string) {
conn.db = name
}
// Database returns the current selected database in the connection.
func (conn *Conn) Database() string {
return conn.db
}
// ID returns the creation ID of the connection.
func (conn *Conn) ID() uint32 {
return conn.uid
}
// UUID returns the UUID of the connection.
func (conn *Conn) UUID() uuid.UUID {
return conn.uuid
}
// Timestamp returns the creation time of the connection.
func (conn *Conn) Timestamp() time.Time {
return conn.ts
}
// SetSpanContext sets the tracer span context to the connection.
func (conn *Conn) SetSpanContext(span tracer.Context) {
conn.Context = span
}
// SpanContext returns the tracer span context of the connection.
func (conn *Conn) SpanContext() tracer.Context {
return conn.Context
}