This repository was archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient.go
More file actions
165 lines (136 loc) · 3.68 KB
/
client.go
File metadata and controls
165 lines (136 loc) · 3.68 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Copyright 2013 Tristan Wietsma
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 jack
import (
"bytes"
"fmt"
"net"
"strconv"
)
type connectionPoolError struct {
desc string
}
func (e connectionPoolError) Error() string {
return fmt.Sprintf("Connection Pool Error: %s", e.desc)
}
var maxConnectionsError = connectionPoolError{"Maximum connections reached."}
// The ConnectionPool struct maintains a finite number of connections for use on client-side.
type ConnectionPool struct {
address string
port uint
size uint
count uint
free []*Connection
}
// NewConnectionPool constructs a ConnectionPool for a given address, port number, and pool size.
func NewConnectionPool(address string, port, size uint) *ConnectionPool {
return &ConnectionPool{
address: address,
port: port,
size: size,
}
}
// Connect gets a connection from the pool
func (cp *ConnectionPool) Connect() (*Connection, error) {
if cp.count == cp.size && len(cp.free) == 0 {
return nil, maxConnectionsError
}
if len(cp.free) > 0 {
sc := cp.free[0]
cp.free = cp.free[1:]
return sc, nil
}
cp.count++
sc, err := NewConnection(cp.address, cp.port)
if err != nil {
return nil, err
}
return sc, nil
}
// Free sends a connection back to the pool
func (cp *ConnectionPool) Free(c *Connection) error {
cp.free = append(cp.free, c)
return nil
}
// The Connection struct defines an individual client connection.
type Connection struct {
conn net.Conn
feed chan string
}
func (sc *Connection) transmit(m *Message) {
b := m.Bytes()
_, err := sc.conn.Write(b)
buf := make([]byte, 1024)
WAIT_FOR_SERVER:
_, err = sc.conn.Read(buf)
if err != nil {
panic(err)
}
end := bytes.IndexByte(buf, EOM)
if end < 0 {
panic(ProtocolError{"Message is missing EOM byte."})
}
payload := string(buf[:end])
sc.feed <- payload
if m.cmd == SUB {
goto WAIT_FOR_SERVER
}
}
// Get returns the value associated with a given key.
func (sc *Connection) Get(key string) string {
m := NewGetMessage(key)
go sc.transmit(m)
return <-sc.feed
}
// Set assigns a value to a key.
func (sc *Connection) Set(key, value string) string {
m := NewSetMessage(key, value)
go sc.transmit(m)
return <-sc.feed
}
// Delete removes a key-value pair from the database.
func (sc *Connection) Delete(key string) string {
m := NewDeleteMessage(key)
go sc.transmit(m)
return <-sc.feed
}
// Publish sets a value to a key and triggers a subscriber update.
func (sc *Connection) Publish(key, value string) string {
m := NewPublishMessage(key, value)
go sc.transmit(m)
return <-sc.feed
}
// Subscribe added a channel to the subscription list on a key.
func (sc *Connection) Subscribe(key string, recv chan<- string) {
m := NewSubscribeMessage(key)
go sc.transmit(m)
for {
recv <- <-sc.feed
}
}
// Close closes a connection.
func (sc *Connection) Close() error {
err := sc.conn.Close()
return err
}
// NewConnection returns a connection to a given address and port number.
func NewConnection(address string, port uint) (*Connection, error) {
fullAddress := address + ":" + strconv.FormatUint(uint64(port), 10)
conn, err := net.Dial("tcp", fullAddress)
sc := Connection{}
if err == nil {
sc.conn = conn
}
sc.feed = make(chan string)
return &sc, err
}