micro--go-micro
c7657f73f4
goreleaser / goreleaser (push) Has been cancelled
* test(harness): read agent plan from the scoped store
The store-scoping change moved an agent's plan from the default table
key agent/{name}/plan to its own table (database "agent", table {name},
key "plan"). The plan-delegate harness tests still read the old key and
failed with 'not found'; read through store.Scope(mem, "agent", name)
like the agent does.
* docs: orient agents-first across README, landing, and docs overview
Lead with agents (then services and flows), surface MCP + A2A as the
interop story, and frame agents as services. Landing hero and feature
grid reordered agents-first with an A2A gateway card.
* v6: module path go-micro.dev/v6, TLS secure by default, NewService
Cut v6. Three breaking changes, bundled so the major bump is paid once:
- Module path go-micro.dev/v5 -> go-micro.dev/v6 across all imports + go.mod.
- TLS verification on by default (was off). MICRO_TLS_SECURE removed;
MICRO_TLS_INSECURE=true opts out for self-signed/dev.
- micro.NewService(name, opts...) is the canonical service constructor,
symmetric with NewAgent/NewFlow; micro.New kept as a deprecated alias;
the old name-less NewService(opts...) removed. Generators emit NewService.
Also ports the JWT auth token provider in-module (go-micro.dev/v6/auth/jwt/token
on golang-jwt/jwt/v5), dropping the v5-pinned github.com/micro/plugins/v5/auth/jwt
and the deprecated dgrijalva/jwt-go.
Docs/README/landing updated to v6 and @latest; v5->v6 migration guide added;
CHANGELOG cut as [6.0.0]. Blog posts left at their historical versions.
---------
Co-authored-by: Claude <noreply@anthropic.com>
160 行
2.8 KiB
Go
160 行
2.8 KiB
Go
package pool
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"go-micro.dev/v6/transport"
|
|
)
|
|
|
|
type pool struct {
|
|
tr transport.Transport
|
|
|
|
closeTimeout time.Duration
|
|
conns map[string][]*poolConn
|
|
mu sync.Mutex
|
|
size int
|
|
ttl time.Duration
|
|
}
|
|
|
|
type poolConn struct {
|
|
transport.Client
|
|
|
|
closeTimeout time.Duration
|
|
created time.Time
|
|
id string
|
|
}
|
|
|
|
func newPool(options Options) *pool {
|
|
return &pool{
|
|
size: options.Size,
|
|
tr: options.Transport,
|
|
ttl: options.TTL,
|
|
closeTimeout: options.CloseTimeout,
|
|
conns: make(map[string][]*poolConn),
|
|
}
|
|
}
|
|
|
|
func (p *pool) Close() error {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
|
|
var err error
|
|
|
|
for k, c := range p.conns {
|
|
for _, conn := range c {
|
|
if nerr := conn.close(); nerr != nil {
|
|
err = nerr
|
|
}
|
|
}
|
|
|
|
delete(p.conns, k)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// NoOp the Close since we manage it.
|
|
func (p *poolConn) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (p *poolConn) Id() string {
|
|
return p.id
|
|
}
|
|
|
|
func (p *poolConn) Created() time.Time {
|
|
return p.created
|
|
}
|
|
|
|
func (p *pool) Get(addr string, opts ...transport.DialOption) (Conn, error) {
|
|
p.mu.Lock()
|
|
conns := p.conns[addr]
|
|
|
|
// While we have conns check age and then return one
|
|
// otherwise we'll create a new conn
|
|
for len(conns) > 0 {
|
|
conn := conns[len(conns)-1]
|
|
conns = conns[:len(conns)-1]
|
|
p.conns[addr] = conns
|
|
|
|
// If conn is old kill it and move on
|
|
if d := time.Since(conn.Created()); d > p.ttl {
|
|
if err := conn.close(); err != nil {
|
|
p.mu.Unlock()
|
|
c, errConn := p.newConn(addr, opts)
|
|
if errConn != nil {
|
|
return nil, errConn
|
|
}
|
|
return c, err
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
// We got a good conn, lets unlock and return it
|
|
p.mu.Unlock()
|
|
|
|
return conn, nil
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
return p.newConn(addr, opts)
|
|
}
|
|
|
|
func (p *pool) newConn(addr string, opts []transport.DialOption) (Conn, error) {
|
|
// create new conn
|
|
c, err := p.tr.Dial(addr, opts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &poolConn{
|
|
Client: c,
|
|
id: uuid.New().String(),
|
|
closeTimeout: p.closeTimeout,
|
|
created: time.Now(),
|
|
}, nil
|
|
}
|
|
|
|
func (p *pool) Release(conn Conn, err error) error {
|
|
// don't store the conn if it has errored
|
|
if err != nil {
|
|
return conn.(*poolConn).close()
|
|
}
|
|
|
|
// otherwise put it back for reuse
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
|
|
conns := p.conns[conn.Remote()]
|
|
if len(conns) >= p.size {
|
|
return conn.(*poolConn).close()
|
|
}
|
|
|
|
p.conns[conn.Remote()] = append(conns, conn.(*poolConn))
|
|
|
|
return nil
|
|
}
|
|
|
|
func (p *poolConn) close() error {
|
|
ch := make(chan error)
|
|
go func() {
|
|
defer close(ch)
|
|
ch <- p.Client.Close()
|
|
}()
|
|
t := time.NewTimer(p.closeTimeout)
|
|
var err error
|
|
select {
|
|
case <-t.C:
|
|
err = errors.New("unable to close in time")
|
|
case err = <-ch:
|
|
t.Stop()
|
|
}
|
|
return err
|
|
}
|