-
Notifications
You must be signed in to change notification settings - Fork 661
/
messenger_server.go
39 lines (31 loc) · 983 Bytes
/
messenger_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package messenger
import (
"context"
"errors"
"github.com/ava-labs/avalanchego/snow/engine/common"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/messenger/messengerproto"
)
var (
errFullQueue = errors.New("full message queue")
_ messengerproto.MessengerServer = &Server{}
)
// Server is a messenger that is managed over RPC.
type Server struct {
messengerproto.UnimplementedMessengerServer
messenger chan<- common.Message
}
// NewServer returns a messenger connected to a remote channel
func NewServer(messenger chan<- common.Message) *Server {
return &Server{messenger: messenger}
}
func (s *Server) Notify(_ context.Context, req *messengerproto.NotifyRequest) (*messengerproto.NotifyResponse, error) {
msg := common.Message(req.Message)
select {
case s.messenger <- msg:
return &messengerproto.NotifyResponse{}, nil
default:
return nil, errFullQueue
}
}