Skip to content

Instantly share code, notes, and snippets.

@feynon
Created July 27, 2021 15:07
Show Gist options
  • Save feynon/d026ad69ccbb4c1850e64e70e2f32978 to your computer and use it in GitHub Desktop.
Save feynon/d026ad69ccbb4c1850e64e70e2f32978 to your computer and use it in GitHub Desktop.
type Worker struct {
cfg WorkerConfig
masterConn *grpc.ClientConn
masterCli proto.JobQueueClient
}
// NewWorker creates a new Worker instance with the specified configuration.
func NewWorker(cfg WorkerConfig) (*Worker, error) {
if err := cfg.Validate(); err != nil {
return nil, xerrors.Errorf("worker config validation failed: %w", err)
}
return &Worker{cfg: cfg}, nil
}
// Dial establishes a connection to the master node.
func (w *Worker) Dial(masterEndpoint string, dialTimeout time.Duration) error {
var dialCtx context.Context
if dialTimeout != 0 {
var cancelFn func()
dialCtx, cancelFn = context.WithTimeout(context.Background(), dialTimeout)
defer cancelFn()
}
conn, err := grpc.DialContext(dialCtx, masterEndpoint, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return xerrors.Errorf("unable to dial master: %w", err)
}
w.masterConn = conn
w.masterCli = proto.NewJobQueueClient(conn)
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment