AmitKumarDas / decide

Apache License 2.0
0 stars 1 forks source link

optional method interface via type casting and/or closures #15

Open AmitKumarDas opened 6 years ago

AmitKumarDas commented 6 years ago
 type eventHandle interface {
     eventHookTypeA
     eventHookTypeB
     // DON'T add TypeC interface.
     someOtherMethod() results
 }
 type eventHookTypeA interface {
     // method definitions
     HandleEventA() results
 }
 type eventHookTypeB interface {
     // method definitions
     HandleEventB() results
 }
 type eventHookTypeC interface {
     HandleEventC() results
 }

 func handleEvents(eventType string, handle eventHandle) results {
      if eventType == "eventA" {
         return handle.HandleEventA()
      } else if eventType == "eventB" {
         return handle.HandleEventB()
      } else if eventType == "eventC" {
         if c,ok := handle.(eventHookTypeC); ok {
             return c.HandleEventC()
         } else {
             log.Println("somewhat bad happen")
         }
      }
      return nil
 }
AmitKumarDas commented 6 years ago

An approach that some code pieces in Kubernetes follows is:

 // LeaseDuration is the duration that non-leader candidates will
 // wait to force acquire leadership. This is measured against time of
 // last observed ack. Defaults to 15 seconds.
 func LeaseDuration(leaseDuration time.Duration) func(*ProvisionController) error {
    return func(c *ProvisionController) error {
        if c.HasRun() {
            return errRuntime
        }
        c.leaseDuration = leaseDuration
        return nil
    }
 }