/
server.go
114 lines (96 loc) · 2.75 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package libp2pserver
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"time"
logging "github.com/ipfs/go-log/v2"
"github.com/ipni/go-libipni/apierror"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-msgio"
"google.golang.org/protobuf/proto"
)
// Idle time before the stream is closed
const streamIdleTimeout = time.Minute
var log = logging.Logger("indexer/libp2p")
type Handler interface {
HandleMessage(ctx context.Context, msgPeer peer.ID, msgbytes []byte) (proto.Message, error)
ProtocolID() protocol.ID
}
// Server handles client requests over libp2p
type Server struct {
ctx context.Context
handler Handler
selfID peer.ID
}
// ID returns the peer.ID of the protocol server.
func (s *Server) ID() peer.ID {
return s.selfID
}
// New creates a new libp2p Server
func New(ctx context.Context, h host.Host, messageHandler Handler) *Server {
s := &Server{
ctx: ctx,
handler: messageHandler,
selfID: h.ID(),
}
// Set handler for each announced protocol
h.SetStreamHandler(messageHandler.ProtocolID(), s.handleNewStream)
return s
}
func HandleError(err error, reqType string) *apierror.Error {
var apierr *apierror.Error
if errors.As(err, &apierr) {
if apierr.Status() >= 500 {
log.Errorw(fmt.Sprint("cannot handle", reqType, "request"), "err", apierr.Error(), "status", apierr.Status())
// Log the error and return only the 5xx status.
return apierror.New(nil, apierr.Status())
}
} else {
apierr = apierror.New(err, http.StatusBadRequest)
}
log.Infow(fmt.Sprint("bad", reqType, "request"), "err", apierr.Error(), "status", apierr.Status())
return apierr
}
// handleNewStream implements the network.StreamHandler
func (s *Server) handleNewStream(stream network.Stream) {
if s.handleNewMessages(stream) {
// If we exited without error, close gracefully.
_ = stream.Close()
} else {
// otherwise, send an error.
_ = stream.Reset()
}
}
// Returns true on orderly completion of writes (so we can Close the stream conveniently).
func (s *Server) handleNewMessages(stream network.Stream) bool {
ctx := s.ctx
handler := s.handler
r := msgio.NewVarintReaderSize(stream, network.MessageSizeMax)
mPeer := stream.Conn().RemotePeer()
timer := time.AfterFunc(streamIdleTimeout, func() { _ = stream.Reset() })
defer timer.Stop()
for {
msgbytes, err := r.ReadMsg()
if err != nil {
r.ReleaseMsg(msgbytes)
return err == io.EOF
}
timer.Reset(streamIdleTimeout)
resp, err := handler.HandleMessage(ctx, mPeer, msgbytes)
r.ReleaseMsg(msgbytes)
if err != nil {
return true
}
// send out response msg
err = writeMsg(stream, resp)
if err != nil {
return false
}
}
}