1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package main
- /*
- import (
- "context"
- "log"
- "math/rand"
- "time"
- "git.scraperwall.com/scw/data"
- "google.golang.org/grpc"
- pb "git.scraperwall.com/scw/grpc/scw"
- )
- // ScwRPC sends incoming requests to an RPC server
- type ScwRPC struct {
- Connection *grpc.ClientConn
- Client pb.SCWProtosClient
- RChan chan *data.Request
- RChanCap int
- }
- // Connect creates a new connection to the RPC server
- func (s *ScwRPC) Connect(addr string) error {
- var err error
- s.Connection, err = grpc.Dial(addr, grpc.WithInsecure())
- if err != nil {
- return err
- }
- // defer conn.Close()
- s.Client = pb.NewSCWProtosClient(s.Connection)
- return nil
- }
- func (s *ScwRPC) sendRequest(req *data.Request) {
- ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
- _, err := s.Client.SendRequest(ctx, req)
- defer cancel()
- if err != nil {
- if rand.Intn(100) == 0 {
- log.Printf("sending request failed: %s\n", err)
- }
- return
- }
- }
- // NewScwRPC generates a new ScwRPC instance
- func NewScwRPC(addr string) (*ScwRPC, error) {
- s := &ScwRPC{}
- err := s.Connect(addr)
- if err != nil {
- return nil, err
- }
- s.RChanCap = 5000
- s.RChan = make(chan *data.Request, s.RChanCap)
- go func() {
- for r := range s.RChan {
- go s.sendRequest(r)
- }
- }()
- return s, nil
- }
- */
|