-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
58 lines (48 loc) · 1.34 KB
/
api.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
/*
* Copyright (c) 2022-2023, Dana Burkart <dana.burkart@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
package fossil
import (
"github.com/dburkart/fossil/pkg/database"
"github.com/dburkart/fossil/pkg/proto"
)
type Client interface {
Open(proto.ConnectionString, uint) error
Close() error
Send(proto.Message) (proto.Message, error)
Append(string, []byte) error
Query(string) (database.Entries, error)
}
// NewClient creates a new Client struct which can be used to interact with a
// remote fossil database. The client is thread safe, but only holds one
// connection at a time. For a client pool, use NewClientPool instead.
func NewClient(connstr string) (Client, error) {
client, err := NewClientPool(connstr, 1)
if err != nil {
return nil, err
}
return client, nil
}
// NewClientPool creates a new Client struct which holds a pool of net.Conn
// resources open to a remote fossil database. This is useful for sending large
// volumes of data to fossil.
func NewClientPool(connstr string, size uint) (Client, error) {
var client Client
var err error
target, err := proto.ParseConnectionString(connstr)
if err != nil {
return nil, err
}
if target.Local == true {
client = &LocalClient{}
} else {
client = &RemoteClient{}
}
err = client.Open(target, size)
if err != nil {
return nil, err
}
return client, nil
}