storj/private/server/prefix.go
Márton Elek 7e71986493 storagenode: accept HTTP calls on public port, listening for monitoring requests
Today each storagenode should have a port which is opened for the internet, and handles DRPC protocol calls.

When we do a HTTP call on the DRPC endpoint, it hangs until a timeout.

This patch changes the behavior: the main DRPC port of the storagenodes can accept HTTP requests and can be used to monitor the status of the node:

 * if returns with HTTP 200 only if the storagnode is healthy (not suspended / disqualified + online score > 0.9)
 * it CAN include information about the current status (per satellite). It's opt-in, you should configure it so.

In this way it becomes extremely easy to monitor storagenodes with external uptime services.

Note: this patch exposes some information which was not easily available before (especially the node status, and used satellites). I think it should be acceptable:

 * Until having more community satellites, all storagenodes are connected to the main Storj satellites.
 * With community satellites, it's good thing to have more transparency (easy way to check who is connected to which satellites)

The implementation is based on this line:

```
http.Serve(NewPrefixedListener([]byte("GET / HT"), publicMux.Route("GET / HT")), p.public.http)
```

This line answers to the TCP requests with `GET / HT...` (GET HTTP request to the route), but puts back the removed prefix.

Change-Id: I3700c7e24524850825ecdf75a4bcc3b4afcb3a74
2022-08-26 09:38:09 +00:00

52 lines
1.0 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package server
import (
"bytes"
"io"
"net"
)
type prefixConn struct {
io.Reader
net.Conn
}
func newPrefixConn(data []byte, conn net.Conn) *prefixConn {
return &prefixConn{
Reader: io.MultiReader(bytes.NewReader(data), conn),
Conn: conn,
}
}
func (pc *prefixConn) Read(p []byte) (n int, err error) {
return pc.Reader.Read(p)
}
// PrefixedListener injects prefix bytes to the beginning of every new connection.
type PrefixedListener struct {
net.Listener
prefix []byte
}
// NewPrefixedListener creates a new PrefixedListener.
func NewPrefixedListener(prefix []byte, listener net.Listener) net.Listener {
return &PrefixedListener{
Listener: listener,
prefix: prefix,
}
}
// Accept implements function of net.Listener.
func (p *PrefixedListener) Accept() (net.Conn, error) {
conn, err := p.Listener.Accept()
if err != nil {
return conn, err
}
return newPrefixConn(p.prefix, conn), nil
}
var _ net.Listener = &PrefixedListener{}