-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removing CID Lists part one: unblocking technical obstacles (#292)
* feat(channels): remove cid lists for sender remove a synchronous disk based lookup of CID lists * feat(channels): don't block on checking cid lists * fix(channels): fix potential race in block index cache
- Loading branch information
1 parent
b442027
commit eddbf61
Showing
16 changed files
with
301 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package channels | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
|
||
datatransfer "github.com/filecoin-project/go-data-transfer" | ||
) | ||
|
||
type readOriginalFn func(datatransfer.ChannelID) (int64, error) | ||
|
||
type blockIndexKey struct { | ||
evt datatransfer.EventCode | ||
chid datatransfer.ChannelID | ||
} | ||
type blockIndexCache struct { | ||
lk sync.RWMutex | ||
values map[blockIndexKey]*int64 | ||
} | ||
|
||
func newBlockIndexCache() *blockIndexCache { | ||
return &blockIndexCache{ | ||
values: make(map[blockIndexKey]*int64), | ||
} | ||
} | ||
|
||
func (bic *blockIndexCache) getValue(evt datatransfer.EventCode, chid datatransfer.ChannelID, readFromOriginal readOriginalFn) (*int64, error) { | ||
idxKey := blockIndexKey{evt, chid} | ||
bic.lk.RLock() | ||
value := bic.values[idxKey] | ||
bic.lk.RUnlock() | ||
if value != nil { | ||
return value, nil | ||
} | ||
bic.lk.Lock() | ||
defer bic.lk.Unlock() | ||
value = bic.values[idxKey] | ||
if value != nil { | ||
return value, nil | ||
} | ||
newValue, err := readFromOriginal(chid) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bic.values[idxKey] = &newValue | ||
return &newValue, nil | ||
} | ||
|
||
func (bic *blockIndexCache) updateIfGreater(evt datatransfer.EventCode, chid datatransfer.ChannelID, newIndex int64, readFromOriginal readOriginalFn) (bool, error) { | ||
value, err := bic.getValue(evt, chid, readFromOriginal) | ||
if err != nil { | ||
return false, err | ||
} | ||
for { | ||
currentIndex := atomic.LoadInt64(value) | ||
if newIndex <= currentIndex { | ||
return false, nil | ||
} | ||
if atomic.CompareAndSwapInt64(value, currentIndex, newIndex) { | ||
return true, nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.