grpc/transport/inproc/inproc.go
Adphi df505b58d7
rename module grpc-toolkit
Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
2023-07-08 01:33:10 +02:00

54 lines
761 B
Go

package inproc
import (
"errors"
"net"
"sync"
"github.com/fullstorydev/grpchan/inprocgrpc"
"go.linka.cloud/grpc-toolkit/transport"
)
var (
_ transport.Transport = &InProc{}
)
func New() transport.Transport {
return &InProc{stop: make(chan struct{})}
}
type InProc struct {
inprocgrpc.Channel
stop chan struct{}
running bool
mu sync.RWMutex
}
func (i *InProc) Serve(_ net.Listener) error {
i.mu.RLock()
running := i.running
i.mu.RUnlock()
if running {
return errors.New("already running")
}
i.mu.Lock()
i.running = true
i.mu.Unlock()
<-i.stop
return nil
}
func (i *InProc) Stop() {
i.mu.RLock()
running := i.running
i.mu.RUnlock()
if running {
i.stop <- struct{}{}
}
}
func (i *InProc) GracefulStop() {
i.Stop()
}