forked from lleiiell/tgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdao_es_factory.go
86 lines (66 loc) · 1.69 KB
/
dao_es_factory.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
package tgo
import (
"net/http"
"sync"
"time"
"github.com/jolestar/go-commons-pool"
"gopkg.in/olivere/elastic.v3"
)
var (
esTransport *http.Transport
esTransportMux sync.Mutex
)
type DaoESFactory struct {
}
func (f *DaoESFactory) MakeObject() (*pool.PooledObject, error) {
client, err := f.MakeClient()
return pool.NewPooledObject(client), err
}
func (f *DaoESFactory) MakeClient() (*elastic.Client, error) {
config := configESGet()
if esTransport == nil {
esTransportMux.Lock()
defer esTransportMux.Unlock()
if esTransport == nil {
esTransport = &http.Transport{
MaxIdleConnsPerHost: config.TransportMaxIdel,
}
}
}
clientHttp := &http.Client{
Transport: esTransport,
Timeout: time.Duration(config.Timeout) * time.Millisecond,
}
client, err := elastic.NewClient(elastic.SetHttpClient(clientHttp), elastic.SetURL(config.Address...))
if err != nil {
// Handle error
UtilLogErrorf("es connect error :%s,address:%v", err.Error(), config.Address)
return nil, err
}
return client, err
}
func (f *DaoESFactory) DestroyObject(object *pool.PooledObject) error {
//do destroy
return nil
}
func (f *DaoESFactory) ValidateObject(object *pool.PooledObject) bool {
//do validate
esClient, ok := object.Object.(*elastic.Client)
if !ok {
UtilLogInfo("es pool validate object failed,convert to client failed")
return false
}
if !esClient.IsRunning() {
UtilLogInfo("es pool validate object failed,convert to client failed")
return false
}
return true
}
func (f *DaoESFactory) ActivateObject(object *pool.PooledObject) error {
//do activate
return nil
}
func (f *DaoESFactory) PassivateObject(object *pool.PooledObject) error {
//do passivate
return nil
}