You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
713 B
Go
37 lines
713 B
Go
8 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"net"
|
||
|
"sync"
|
||
|
"google.golang.org/grpc"
|
||
|
"golang.org/x/net/context"
|
||
|
"github.com/jordanorelli/grpc-ui/lib/count"
|
||
|
)
|
||
|
|
||
|
type countServer struct {
|
||
|
sync.Mutex
|
||
|
last int64
|
||
|
}
|
||
|
|
||
|
func (c *countServer) Next(ctx context.Context, r *count.NextRequest) (*count.NextReply, error) {
|
||
|
return &count.NextReply{Val: c.incr()}, nil
|
||
|
}
|
||
|
|
||
|
func (c *countServer) incr() int64 {
|
||
|
c.Lock()
|
||
|
defer c.Unlock()
|
||
|
|
||
|
c.last += 1
|
||
|
return c.last
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
lis, err := net.Listen("tcp", "localhost:9001")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
s := grpc.NewServer()
|
||
|
count.RegisterCountServer(s, &countServer{})
|
||
|
s.Serve(lis)
|
||
|
}
|