Skip to content

Instantly share code, notes, and snippets.

@audrenbdb
Created October 8, 2022 09:04
Show Gist options
  • Save audrenbdb/4df8b78aaa43692f891fb4628daf5917 to your computer and use it in GitHub Desktop.
Save audrenbdb/4df8b78aaa43692f891fb4628daf5917 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func main() {
conn, err := grpc.DialContext(context.Background(), "localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal(err)
}
defer conn.Close()
exp := time.After(50 * time.Second)
ticker := time.NewTicker(1 * time.Second)
for {
select {
case <-exp:
ticker.Stop()
return
case <-ticker.C:
fmt.Printf("Connection status: %s\n", conn.GetState().String())
}
}
}
package main
import (
"log"
"net"
"google.golang.org/grpc"
)
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
s.Serve(lis)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment