func main() { sig := runOutput() on := fmt.Sprintf(":%d", comm.Port()) cert, err := tls.X509KeyPair(comm.Cert(), comm.Key()) if err != nil { log.Fatal("failed to load cert and key", err) } creds := credentials.NewServerTLSFromCert(&cert) s := grpc.NewServer( grpc.Creds(creds), ) svr := &server{sig: sig} comm.RegisterGarageServer(s, svr) listener, err := net.Listen("tcp", on) if err != nil { log.Fatal("failed to listen", err) } err = s.Serve(listener) if err != nil { log.Fatal("failed to serve", err) } }
func (vs *viewState) startConn() { certpool := x509.NewCertPool() if !certpool.AppendCertsFromPEM(comm.CA()) { // Add CA public cert. panic("failed to add cert") } creds := credentials.NewTLS(&tls.Config{ RootCAs: certpool, }) errClosed := errors.New("closed") backer := backoff.New(time.Millisecond*500, time.Millisecond*10) do := func() error { conn, err := grpc.Dial(fmt.Sprintf("%s:%d", comm.Host(), comm.Port()), grpc.WithTimeout(time.Second*3), grpc.FailOnNonTempDialError(true), grpc.WithTransportCredentials(creds), ) if err != nil { return err } defer conn.Close() client := comm.NewGarageClient(conn) ctx := context.TODO() noop := &comm.Noop{} _, err = client.Ping(ctx, noop) if err != nil { return err } vs.mu.Lock() vs.connErr = nil vs.pingOk = true vs.mu.Unlock() ticker := time.NewTicker(time.Millisecond * 1500) defer ticker.Stop() for { select { case <-vs.onClose: return errClosed case tm := <-vs.toggle: if tm.Add(time.Second * 4).Before(time.Now()) { continue } _, err = client.Toggle(ctx, noop) if err != nil { return err } case <-ticker.C: _, err = client.Ping(ctx, noop) if err != nil { return err } vs.mu.Lock() vs.pingOk = true vs.mu.Unlock() } } } for { err := do() if err == errClosed { return } if err != nil { vs.mu.Lock() vs.connErr = err vs.pingOk = false vs.mu.Unlock() time.Sleep(backer.Duration()) continue } backer.Reset() continue } }