Giter Club home page Giter Club logo

Comments (4)

afex avatar afex commented on July 30, 2024

no plans to implement it in the near future, but would certainly be open for a discussion on how it might be designed.

from hystrix-go.

isaldana avatar isaldana commented on July 30, 2024

I was able to isolate those calls in another library just fine. It would be cool if this library had everything Hystrix provides. I was initially thinking to create an interface with IsCacheable() and GroupKey() and something like DoWork() for the main callback and Fallback() for the fallback function. DoWork() would return an interface{} and if IsCacheable() is true, it can be stored in an LRU cache. Next time DoWork() is called, it would check the cache. GroupKey() would be to wrap DoWork() with AddRequest() in the following:

type requestPool struct {
    pendingRequests     map[string][]chan interface{}
    pendingRequestsLock sync.Mutex
}

// Groups requests in a queue and only call the callback of the first request.
// The result of that first request will be sent to the waiting requests.
func (r *requestPool) AddRequest(key string, callback func() interface{}) chan interface{} {
    ch := make(chan interface{})
    r.pendingRequestsLock.Lock()
    r.pendingRequests[key] = append(r.pendingRequests[key], ch)
    requestNumber := len(r.pendingRequests[key])
    r.pendingRequestsLock.Unlock()

    // Call callback if it's first request
    if requestNumber == 1 {
        go func() {
            resp := callback()
            r.sendResponse(key, resp)
        }()
    }
    return ch
}

// Send response to all waiting requests in the same group
func (r *requestPool) sendResponse(key string, res interface{}) {
    go func() {
        r.pendingRequestsLock.Lock()
        for _, ch := range r.pendingRequests[key] {
            ch <- res
        }
        delete(r.pendingRequests, key)
        r.pendingRequestsLock.Unlock()
    }()
}

from hystrix-go.

afex avatar afex commented on July 30, 2024

avoiding the run/fallback functions returning an empty interface is done on purpose, and is why they make no attempt to handle responses. requiring users to constantly cast results to their expected type is not a good API. an early version of hystrix-go took this approach and was awful to use.

i'm open to adopting these features from java hystrix, but not at the expense of the usability of the library when trying to copy java's use of generics.

from hystrix-go.

afex avatar afex commented on July 30, 2024

closing this issue, but still open to ideas around collapsing commands and caching results

from hystrix-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.