github.com/ledgerwatch/erigon-lib@v1.0.0/direct/sentinel_client.go (about)

     1  /*
     2     Copyright 2021 Erigon contributors
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package direct
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  
    23  	"github.com/ledgerwatch/erigon-lib/gointerfaces/sentinel"
    24  	"google.golang.org/grpc"
    25  )
    26  
    27  type SentinelClientDirect struct {
    28  	server sentinel.SentinelServer
    29  }
    30  
    31  func NewSentinelClientDirect(sentinel sentinel.SentinelServer) sentinel.SentinelClient {
    32  	return &SentinelClientDirect{server: sentinel}
    33  }
    34  
    35  func (s *SentinelClientDirect) SendRequest(ctx context.Context, in *sentinel.RequestData, opts ...grpc.CallOption) (*sentinel.ResponseData, error) {
    36  	return s.server.SendRequest(ctx, in)
    37  }
    38  
    39  func (s *SentinelClientDirect) SetStatus(ctx context.Context, in *sentinel.Status, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
    40  	return s.server.SetStatus(ctx, in)
    41  }
    42  
    43  func (s *SentinelClientDirect) GetPeers(ctx context.Context, in *sentinel.EmptyMessage, opts ...grpc.CallOption) (*sentinel.PeerCount, error) {
    44  	return s.server.GetPeers(ctx, in)
    45  }
    46  
    47  func (s *SentinelClientDirect) BanPeer(ctx context.Context, p *sentinel.Peer, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
    48  	return s.server.BanPeer(ctx, p)
    49  }
    50  func (s *SentinelClientDirect) UnbanPeer(ctx context.Context, p *sentinel.Peer, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
    51  	return s.server.UnbanPeer(ctx, p)
    52  }
    53  func (s *SentinelClientDirect) RewardPeer(ctx context.Context, p *sentinel.Peer, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
    54  	return s.server.RewardPeer(ctx, p)
    55  }
    56  func (s *SentinelClientDirect) PenalizePeer(ctx context.Context, p *sentinel.Peer, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
    57  	return s.server.PenalizePeer(ctx, p)
    58  }
    59  
    60  func (s *SentinelClientDirect) PublishGossip(ctx context.Context, in *sentinel.GossipData, opts ...grpc.CallOption) (*sentinel.EmptyMessage, error) {
    61  	return s.server.PublishGossip(ctx, in)
    62  }
    63  
    64  // Subscribe gossip part. the only complex section of this bullshit
    65  
    66  func (s *SentinelClientDirect) SubscribeGossip(ctx context.Context, in *sentinel.EmptyMessage, opts ...grpc.CallOption) (sentinel.Sentinel_SubscribeGossipClient, error) {
    67  	ch := make(chan *gossipReply, 16384)
    68  	streamServer := &SentinelSubscribeGossipS{ch: ch, ctx: ctx}
    69  	go func() {
    70  		defer close(ch)
    71  		streamServer.Err(s.server.SubscribeGossip(in, streamServer))
    72  	}()
    73  	return &SentinelSubscribeGossipC{ch: ch, ctx: ctx}, nil
    74  }
    75  
    76  type SentinelSubscribeGossipC struct {
    77  	ch  chan *gossipReply
    78  	ctx context.Context
    79  	grpc.ClientStream
    80  }
    81  
    82  func (c *SentinelSubscribeGossipC) Recv() (*sentinel.GossipData, error) {
    83  	m, ok := <-c.ch
    84  	if !ok || m == nil {
    85  		return nil, io.EOF
    86  	}
    87  	return m.r, m.err
    88  }
    89  func (c *SentinelSubscribeGossipC) Context() context.Context { return c.ctx }
    90  
    91  type SentinelSubscribeGossipS struct {
    92  	ch  chan *gossipReply
    93  	ctx context.Context
    94  	grpc.ServerStream
    95  }
    96  
    97  type gossipReply struct {
    98  	r   *sentinel.GossipData
    99  	err error
   100  }
   101  
   102  func (s *SentinelSubscribeGossipS) Send(m *sentinel.GossipData) error {
   103  	s.ch <- &gossipReply{r: m}
   104  	return nil
   105  }
   106  func (s *SentinelSubscribeGossipS) Context() context.Context { return s.ctx }
   107  func (s *SentinelSubscribeGossipS) Err(err error) {
   108  	if err == nil {
   109  		return
   110  	}
   111  	s.ch <- &gossipReply{err: err}
   112  }