37 lines
852 B
Go
37 lines
852 B
Go
package svc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"example.com/composition/internal/middleware"
|
|
"sync/atomic"
|
|
)
|
|
|
|
var Closes atomic.Int64
|
|
var ErrInit = errors.New("init fault")
|
|
|
|
type Client struct{ closed atomic.Bool }
|
|
|
|
func (c *Client) Check(ctx context.Context) error {
|
|
if c.closed.Load() {
|
|
return errors.New("client closed before HTTP drained")
|
|
}
|
|
return ctx.Err()
|
|
}
|
|
func (c *ServiceContext) initClient(ctx context.Context) error {
|
|
c.client = &Client{}
|
|
c.OnClose(func() error {
|
|
if !c.client.closed.CompareAndSwap(false, true) {
|
|
return errors.New("double close")
|
|
}
|
|
Closes.Add(1)
|
|
return nil
|
|
})
|
|
if c.Config.FactoryMode == "initFail" {
|
|
return ErrInit
|
|
}
|
|
return nil
|
|
}
|
|
func (c *ServiceContext) Access() middleware.AccessChecker { return c.client }
|
|
func (c *ServiceContext) FactoryMode() string { return c.Config.FactoryMode }
|