-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpccli.go
109 lines (90 loc) · 2.76 KB
/
grpccli.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
// ## gRPC Client: musayerapi/grpccli.go
//
// There are two kinds of SayerService clients:
//
// 1. SayerClient: a gRPC client for the SayerService
// 2. SayerClientPool: a pool of SayerClients
//
// Both SayerClient and SayerClientPool implement the Sayer interface.
// So they can simply be used as Sayers. (bravo decorator & composite pattern!)
package sayerapigo
import (
"context"
"golang.org/x/exp/slog"
"google.golang.org/grpc"
"github.com/cdfmlr/ellipsis"
"github.com/cdfmlr/pool"
sayerv1 "github.com/murchinroom/sayerapigo/proto"
)
var MaxConsecutiveFailures = 3
// SayerClient is a gRPC client for the SayerService
type SayerClient struct {
client sayerv1.SayerServiceClient
pool.Poolable
failed int // successive failures of Say. SayerClient.Say mantains this value, and should not be modified by other code.
}
// NewSayerClient creates a new SayerClient
func NewSayerClient(addr string) (*SayerClient, error) {
conn, err := grpc.Dial(addr, grpc.WithInsecure())
if err != nil {
return nil, err
}
return &SayerClient{
client: sayerv1.NewSayerServiceClient(conn),
}, nil
}
// Say calls the Say method of the SayerService
//
// Say implements the Sayer interface. So SayerClient can be used
// as a Sayer.
func (c *SayerClient) Say(role string, text string) (format string, audio []byte, err error) {
logger := slog.With("role", ellipsis.Centering(role, 9), "text", ellipsis.Centering(text, 9))
resp, err := c.client.Say(context.Background(), &sayerv1.SayRequest{
Role: role,
Text: text,
})
if err != nil {
logger.Error("[SayerClient] Say (RPC) failed", "err", err)
c.failed++
return "", nil, err
}
c.failed = 0
logger.Info("[SayerClient] Say (RPC) succeeded.")
return resp.Format, resp.Audio, nil
}
// Close closes the SayerClient
func (c *SayerClient) Close() error {
return nil
}
// SayerClientPool is a pool of SayerClients.
//
// SayerClientPool implements the Sayer interface.
// So SayerClientPool can be used as a Sayer.
type SayerClientPool struct {
pool pool.Pool[*SayerClient]
}
func NewSayerClientPool(addr string, size int64) (*SayerClientPool, error) {
p := pool.NewPool(size, func() (*SayerClient, error) {
return NewSayerClient(addr)
})
return &SayerClientPool{
pool: p,
}, nil
}
func (p *SayerClientPool) Say(role string, text string) (format string, audio []byte, err error) {
// get a client from the pool
client, err := p.pool.Get()
if err != nil {
return "", nil, err
}
// call Say on the client
format, audio, err = client.Say(role, text)
if err != nil && client.failed > MaxConsecutiveFailures {
// if the client has failed MaxConsecutiveFailures times in a row, remove it from the pool
p.pool.Release(client)
} else {
// otherwise, put it back into the pool
p.pool.Put(client)
}
return format, audio, nil
}