herald.go/server/server.go

316 lines
8.3 KiB
Go
Raw Normal View History

2021-04-19 21:25:34 +02:00
package server
2021-03-18 22:14:56 +01:00
import (
2021-09-18 19:21:32 +02:00
"context"
2021-10-01 19:54:03 +02:00
"crypto/sha256"
2021-09-18 19:21:32 +02:00
"fmt"
2021-10-01 19:54:03 +02:00
"hash"
2021-06-18 06:01:47 +02:00
"log"
"net"
2021-10-01 19:54:03 +02:00
"net/http"
"os"
2021-06-18 06:01:47 +02:00
"regexp"
"sync"
2021-09-21 20:02:09 +02:00
"time"
2021-10-01 19:54:03 +02:00
"github.com/ReneKroon/ttlcache/v2"
"github.com/lbryio/hub/internal/metrics"
2021-09-24 22:24:22 +02:00
"github.com/lbryio/hub/meta"
2021-03-18 22:14:56 +01:00
pb "github.com/lbryio/hub/protobuf/go"
2021-06-04 07:56:50 +02:00
"github.com/olivere/elastic/v7"
"github.com/prometheus/client_golang/prometheus"
2021-07-06 02:20:38 +02:00
"github.com/prometheus/client_golang/prometheus/promhttp"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
2021-03-18 22:14:56 +01:00
)
2021-04-19 21:25:34 +02:00
type Server struct {
GrpcServer *grpc.Server
Args *Args
MultiSpaceRe *regexp.Regexp
WeirdCharsRe *regexp.Regexp
EsClient *elastic.Client
QueryCache *ttlcache.Cache
S256 *hash.Hash
2021-10-03 04:49:49 +02:00
LastRefreshCheck time.Time
RefreshDelta time.Duration
NumESRefreshes int64
PeerServers map[string]*Peer
PeerServersMut sync.RWMutex
NumPeerServers *int64
PeerSubs map[string]*Peer
PeerSubsMut sync.RWMutex
NumPeerSubs *int64
2021-11-15 14:52:32 +01:00
ExternalIP net.IP
2021-03-18 22:14:56 +01:00
pb.UnimplementedHubServer
}
2021-09-24 22:24:22 +02:00
func getVersion() string {
return meta.Version
2021-07-06 02:20:38 +02:00
}
2021-03-18 22:14:56 +01:00
/*
'blockchain.block.get_chunk'
'blockchain.block.get_header'
'blockchain.estimatefee'
'blockchain.relayfee'
'blockchain.scripthash.get_balance'
'blockchain.scripthash.get_history'
'blockchain.scripthash.get_mempool'
'blockchain.scripthash.listunspent'
'blockchain.scripthash.subscribe'
'blockchain.transaction.broadcast'
'blockchain.transaction.get'
'blockchain.transaction.get_batch'
'blockchain.transaction.info'
'blockchain.transaction.get_merkle'
'server.add_peer'
'server.banner'
'server.payment_address'
'server.donation_address'
'server.features'
'server.peers.subscribe'
'server.version'
'blockchain.transaction.get_height'
'blockchain.claimtrie.search'
'blockchain.claimtrie.resolve'
'blockchain.claimtrie.getclaimsbyids'
'blockchain.block.get_server_height'
'mempool.get_fee_histogram'
'blockchain.block.headers'
'server.ping'
'blockchain.headers.subscribe'
'blockchain.address.get_balance'
'blockchain.address.get_history'
'blockchain.address.get_mempool'
'blockchain.address.listunspent'
'blockchain.address.subscribe'
'blockchain.address.unsubscribe'
*/
func (s *Server) PeerSubsLoadOrStore(peer *Peer) (actual *Peer, loaded bool) {
key := peer.peerKey()
s.PeerSubsMut.RLock()
if actual, ok := s.PeerSubs[key]; ok {
s.PeerSubsMut.RUnlock()
return actual, true
} else {
s.PeerSubsMut.RUnlock()
s.PeerSubsMut.Lock()
s.PeerSubs[key] = peer
s.PeerSubsMut.Unlock()
return peer, false
}
}
func (s *Server) PeerServersLoadOrStore(peer *Peer) (actual *Peer, loaded bool) {
key := peer.peerKey()
s.PeerServersMut.RLock()
if actual, ok := s.PeerServers[key]; ok {
s.PeerServersMut.RUnlock()
return actual, true
} else {
s.PeerServersMut.RUnlock()
s.PeerServersMut.Lock()
s.PeerServers[key] = peer
s.PeerServersMut.Unlock()
return peer, false
}
}
func (s *Server) Run() {
l, err := net.Listen("tcp", ":"+s.Args.Port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
pb.RegisterHubServer(s.GrpcServer, s)
reflection.Register(s.GrpcServer)
log.Printf("listening on %s\n", l.Addr().String())
log.Println(s.Args)
if err := s.GrpcServer.Serve(l); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
// MakeHubServer takes the arguments given to a hub when it's started and
// initializes everything. It loads information about previously known peers,
// creates needed internal data structures, and initializes goroutines.
func MakeHubServer(ctx context.Context, args *Args) *Server {
2021-07-06 02:20:38 +02:00
grpcServer := grpc.NewServer(grpc.NumStreamWorkers(10))
2021-06-01 04:19:10 +02:00
2021-08-24 10:45:30 +02:00
multiSpaceRe, err := regexp.Compile(`\s{2,}`)
2021-06-01 04:19:10 +02:00
if err != nil {
log.Fatal(err)
}
weirdCharsRe, err := regexp.Compile("[#!~]")
if err != nil {
log.Fatal(err)
}
var client *elastic.Client = nil
if !args.DisableEs {
esUrl := args.EsHost + ":" + args.EsPort
opts := []elastic.ClientOptionFunc{
elastic.SetSniff(true),
elastic.SetSnifferTimeoutStartup(time.Second * 60),
elastic.SetSnifferTimeout(time.Second * 60),
elastic.SetURL(esUrl),
}
if args.Debug {
opts = append(opts, elastic.SetTraceLog(log.New(os.Stderr, "[[ELASTIC]]", 0)))
}
client, err = elastic.NewClient(opts...)
if err != nil {
log.Fatal(err)
}
}
2021-10-01 19:54:03 +02:00
cache := ttlcache.NewCache()
err = cache.SetTTL(time.Duration(args.CacheTTL) * time.Minute)
2021-10-01 19:54:03 +02:00
if err != nil {
log.Fatal(err)
}
s256 := sha256.New()
var refreshDelta = time.Second * time.Duration(args.RefreshDelta)
2021-10-03 04:49:49 +02:00
if args.Debug {
refreshDelta = time.Second * 0
}
numPeers := new(int64)
*numPeers = 0
numSubs := new(int64)
*numSubs = 0
2021-08-11 05:39:37 +02:00
s := &Server{
2021-10-03 04:49:49 +02:00
GrpcServer: grpcServer,
Args: args,
MultiSpaceRe: multiSpaceRe,
WeirdCharsRe: weirdCharsRe,
EsClient: client,
QueryCache: cache,
S256: &s256,
2021-10-03 04:49:49 +02:00
LastRefreshCheck: time.Now(),
RefreshDelta: refreshDelta,
2021-10-03 04:49:49 +02:00
NumESRefreshes: 0,
PeerServers: make(map[string]*Peer),
PeerServersMut: sync.RWMutex{},
NumPeerServers: numPeers,
PeerSubs: make(map[string]*Peer),
PeerSubsMut: sync.RWMutex{},
NumPeerSubs: numSubs,
2021-11-25 00:24:06 +01:00
ExternalIP: net.IPv4(127, 0, 0, 1),
}
// Start up our background services
2021-11-25 00:56:34 +01:00
if !args.DisableStartPrometheus {
go s.prometheusEndpoint(s.Args.PrometheusPort, "metrics")
}
2021-11-25 00:56:34 +01:00
if !args.DisableStartUDP {
go func() {
err := UDPServer(args)
if err != nil {
log.Println("UDP Server failed!", err)
}
}()
2021-06-01 04:19:10 +02:00
}
// Load peers from disk and subscribe to one if there are any
2021-11-25 00:56:34 +01:00
if !args.DisableLoadPeers {
go func() {
err := s.loadPeers()
if err != nil {
log.Println(err)
}
}()
}
2021-06-01 04:19:10 +02:00
return s
2021-07-06 02:20:38 +02:00
}
// prometheusEndpoint is a goroutine which start up a prometheus endpoint
// for this hub to allow for metric tracking.
func (s *Server) prometheusEndpoint(port string, endpoint string) {
2021-09-21 20:02:09 +02:00
http.Handle("/"+endpoint, promhttp.Handler())
2021-07-06 02:20:38 +02:00
log.Println(fmt.Sprintf("listening on :%s /%s", port, endpoint))
2021-09-21 20:02:09 +02:00
err := http.ListenAndServe(":"+port, nil)
log.Fatalln("Shouldn't happen??!?!", err)
}
// Hello is a grpc endpoint to allow another hub to tell us about itself.
// The passed message includes information about the other hub, and all
// of its peers which are added to the knowledge of this hub.
func (s *Server) Hello(ctx context.Context, args *pb.HelloMessage) (*pb.HelloMessage, error) {
metrics.RequestsCount.With(prometheus.Labels{"method": "hello"}).Inc()
port := args.Port
host := args.Host
newPeer := &Peer{
2021-12-03 17:52:21 +01:00
Address: host,
Port: port,
LastSeen: time.Now(),
}
log.Println(newPeer)
err := s.addPeer(newPeer, false, true)
// They just contacted us, so this shouldn't happen
if err != nil {
log.Println(err)
}
s.mergePeers(args.Servers)
s.writePeers()
s.notifyPeerSubs(newPeer)
return s.makeHelloMessage(), nil
}
// PeerSubscribe adds a peer hub to the list of subscribers to update about
// new peers.
func (s *Server) PeerSubscribe(ctx context.Context, in *pb.ServerMessage) (*pb.StringValue, error) {
metrics.RequestsCount.With(prometheus.Labels{"method": "peer_subscribe"}).Inc()
var msg = "Success"
peer := &Peer{
2021-12-03 17:52:21 +01:00
Address: in.Address,
Port: in.Port,
LastSeen: time.Now(),
2021-07-06 02:20:38 +02:00
}
if _, loaded := s.PeerSubsLoadOrStore(peer); !loaded {
s.incNumSubs()
metrics.PeersSubscribed.Inc()
} else {
msg = "Already subscribed"
}
return &pb.StringValue{Value: msg}, nil
2021-07-06 02:20:38 +02:00
}
// AddPeer is a grpc endpoint to tell this hub about another hub in the network.
func (s *Server) AddPeer(ctx context.Context, args *pb.ServerMessage) (*pb.StringValue, error) {
metrics.RequestsCount.With(prometheus.Labels{"method": "add_peer"}).Inc()
var msg = "Success"
newPeer := &Peer{
2021-12-03 17:52:21 +01:00
Address: args.Address,
Port: args.Port,
LastSeen: time.Now(),
}
err := s.addPeer(newPeer, true, true)
if err != nil {
log.Println(err)
msg = "Failed"
}
return &pb.StringValue{Value: msg}, err
2021-07-06 02:20:38 +02:00
}
// Ping is a grpc endpoint that returns a short message.
func (s *Server) Ping(ctx context.Context, args *pb.EmptyMessage) (*pb.StringValue, error) {
metrics.RequestsCount.With(prometheus.Labels{"method": "ping"}).Inc()
2021-09-18 19:21:32 +02:00
return &pb.StringValue{Value: "Hello, world!"}, nil
2021-07-06 02:20:38 +02:00
}
// Version is a grpc endpoint to get this hub's version.
func (s *Server) Version(ctx context.Context, args *pb.EmptyMessage) (*pb.StringValue, error) {
metrics.RequestsCount.With(prometheus.Labels{"method": "version"}).Inc()
2021-09-24 22:24:22 +02:00
return &pb.StringValue{Value: getVersion()}, nil
2021-09-18 19:21:32 +02:00
}