grpc/transport/inproc/inproc.go

54 lines
761 B
Go
Raw Permalink Normal View History

2021-09-30 10:00:22 +00:00
package inproc
import (
"errors"
"net"
"sync"
"github.com/fullstorydev/grpchan/inprocgrpc"
"go.linka.cloud/grpc-toolkit/transport"
2021-09-30 10:00:22 +00:00
)
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()
}